Fix uninitalised variable in event.cpp (#5764)
[oweals/minetest.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 Event::Event()
29 {
30 #ifndef USE_CPP11_MUTEX
31 #       if USE_WIN_MUTEX
32         event = CreateEvent(NULL, false, false, NULL);
33 #       else
34         pthread_cond_init(&cv, NULL);
35         pthread_mutex_init(&mutex, NULL);
36         notified = false;
37 #       endif
38 #elif USE_CPP11_MUTEX
39         notified = false;
40 #endif
41 }
42
43 #ifndef USE_CPP11_MUTEX
44 Event::~Event()
45 {
46 #if USE_WIN_MUTEX
47         CloseHandle(event);
48 #else
49         pthread_cond_destroy(&cv);
50         pthread_mutex_destroy(&mutex);
51 #endif
52 }
53 #endif
54
55
56 void Event::wait()
57 {
58 #if USE_CPP11_MUTEX
59         MutexAutoLock lock(mutex);
60         while (!notified) {
61                 cv.wait(lock);
62         }
63         notified = false;
64 #elif USE_WIN_MUTEX
65         WaitForSingleObject(event, INFINITE);
66 #else
67         pthread_mutex_lock(&mutex);
68         while (!notified) {
69                 pthread_cond_wait(&cv, &mutex);
70         }
71         notified = false;
72         pthread_mutex_unlock(&mutex);
73 #endif
74 }
75
76
77 void Event::signal()
78 {
79 #if USE_CPP11_MUTEX
80         MutexAutoLock lock(mutex);
81         notified = true;
82         cv.notify_one();
83 #elif USE_WIN_MUTEX
84         SetEvent(event);
85 #else
86         pthread_mutex_lock(&mutex);
87         notified = true;
88         pthread_cond_signal(&cv);
89         pthread_mutex_unlock(&mutex);
90 #endif
91 }