]> git.lizzy.rs Git - dragonfireclient.git/blob - src/threading/event.cpp
Fix Event implementation
[dragonfireclient.git] / src / threading / event.cpp
1 /*
2 This file is a part of the JThread package, which contains some object-
3 oriented thread wrappers for different thread implementations.
4
5 Copyright (c) 2000-2006  Jori Liesenborgs (jori.liesenborgs@gmail.com)
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "threading/event.h"
27
28 #if defined(_WIN32)
29         #ifndef WIN32_LEAN_AND_MEAN
30                 #define WIN32_LEAN_AND_MEAN
31         #endif
32         #include <windows.h>
33 #endif
34
35
36 #if __cplusplus < 201103L
37 Event::Event()
38 {
39 #ifdef _WIN32
40         event = CreateEvent(NULL, false, false, NULL);
41 #else
42         pthread_cond_init(&cv, NULL);
43         pthread_mutex_init(&mutex, NULL);
44 #endif
45 }
46
47 Event::~Event()
48 {
49 #ifdef _WIN32
50         CloseHandle(event);
51 #else
52         pthread_cond_destroy(&cv);
53         pthread_mutex_destroy(&mutex);
54 #endif
55 }
56 #endif
57
58
59 void Event::wait()
60 {
61 #if __cplusplus >= 201103L
62         MutexAutoLock lock(mutex);
63         while (!notified) {
64                 cv.wait(lock);
65         }
66         notified = false;
67 #elif defined(_WIN32)
68         WaitForSingleObject(event, INFINITE);
69 #else
70         pthread_mutex_lock(&mutex);
71         while (!notified) {
72                 pthread_cond_wait(&cv, &mutex);
73         }
74         notified = false;
75         pthread_mutex_unlock(&mutex);
76 #endif
77 }
78
79
80 void Event::signal()
81 {
82 #if __cplusplus >= 201103L
83         MutexAutoLock lock(mutex);
84         notified = true;
85         cv.notify_one();
86 #elif defined(_WIN32)
87         SetEvent(event);
88 #else
89         pthread_mutex_lock(&mutex);
90         notified = true;
91         pthread_cond_signal(&cv);
92         pthread_mutex_unlock(&mutex);
93 #endif
94 }
95