Return to semaphore.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libstrongswan / threading / windows |
1.1 misho 1: /* 2: * Copyright (C) 2013 Martin Willi 3: * Copyright (C) 2013 revosec AG 4: * 5: * This program is free software; you can redistribute it and/or modify it 6: * under the terms of the GNU General Public License as published by the 7: * Free Software Foundation; either version 2 of the License, or (at your 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. 9: * 10: * This program is distributed in the hope that it will be useful, but 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13: * for more details. 14: */ 15: 16: #include <library.h> 17: #include <threading/semaphore.h> 18: 19: typedef struct private_semaphore_t private_semaphore_t; 20: 21: /** 22: * private data of a semaphore 23: */ 24: struct private_semaphore_t { 25: /** 26: * public interface 27: */ 28: semaphore_t public; 29: 30: /** 31: * Handle to semaphore 32: */ 33: HANDLE handle; 34: }; 35: 36: METHOD(semaphore_t, timed_wait, bool, 37: private_semaphore_t *this, u_int timeout) 38: { 39: /* use alertable wait to allow cancellation */ 40: return WaitForSingleObjectEx(this->handle, timeout, TRUE) == WAIT_TIMEOUT; 41: } 42: 43: METHOD(semaphore_t, timed_wait_abs, bool, 44: private_semaphore_t *this, timeval_t tv) 45: { 46: DWORD timeout; 47: timeval_t now, diff; 48: 49: time_monotonic(&now); 50: if (timercmp(&now, &tv, >)) 51: { 52: return TRUE; 53: } 54: timersub(&tv, &now, &diff); 55: timeout = diff.tv_sec * 1000 + diff.tv_usec / 1000; 56: 57: return timed_wait(this, timeout); 58: } 59: 60: METHOD(semaphore_t, wait_, void, 61: private_semaphore_t *this) 62: { 63: timed_wait(this, INFINITE); 64: } 65: 66: METHOD(semaphore_t, post, void, 67: private_semaphore_t *this) 68: { 69: ReleaseSemaphore(this->handle, 1, NULL); 70: } 71: 72: METHOD(semaphore_t, destroy, void, 73: private_semaphore_t *this) 74: { 75: CloseHandle(this->handle); 76: free(this); 77: } 78: 79: /* 80: * Described in header 81: */ 82: semaphore_t *semaphore_create(u_int value) 83: { 84: private_semaphore_t *this; 85: 86: INIT(this, 87: .public = { 88: .wait = _wait_, 89: .timed_wait = _timed_wait, 90: .timed_wait_abs = _timed_wait_abs, 91: .post = _post, 92: .destroy = _destroy, 93: }, 94: /* our API does not have an upper limit, but Windows requires one. 95: * 0xFFFFFFF (268435455) is the highest value for which Windows does 96: * not return ERROR_INVALID_PARAMETER, and should be sufficient. */ 97: .handle = CreateSemaphore(NULL, value, 0xFFFFFFF, NULL), 98: ); 99: 100: return &this->public; 101: }