ui/winui/condvar.cpp

branch
newapi
changeset 376
5578494147cb
parent 373
2eede3d98aba
equal deleted inserted replaced
375:af087d0fad9b 376:5578494147cb
30 #include "condvar.h" 30 #include "condvar.h"
31 31
32 32
33 33
34 UiCondVar* ui_condvar_create(void) { 34 UiCondVar* ui_condvar_create(void) {
35 UiWinCondVar *var = (UiWinCondVar*)malloc(sizeof(UiWinCondVar)); 35 UiWinCondVar *var = new UiWinCondVar();
36 var->var.data = NULL; 36 var->var.data = NULL;
37 var->var.intdata = 0; 37 var->var.intdata = 0;
38 var->set = 0; 38 var->set = 0;
39 return (UiCondVar*)var; 39 return (UiCondVar*)var;
40 } 40 }
41 41
42 void ui_condvar_wait(UiCondVar *var) { 42 void ui_condvar_wait(UiCondVar *var) {
43 UiWinCondVar *p = (UiWinCondVar*)var; 43 UiWinCondVar *p = (UiWinCondVar*)var;
44 p->mutex.lock(); 44 std::unique_lock<std::mutex> lock(p->mutex);
45
45 if(!p->set) { 46 if(!p->set) {
46 std::unique_lock<std::mutex> lock(p->mutex);
47 p->cond.wait(lock); 47 p->cond.wait(lock);
48 } 48 }
49 p->set = 0; 49 p->set = 0;
50 p->mutex.unlock();
51
52 } 50 }
53 51
54 void ui_condvar_signal(UiCondVar *var, void *data, int intdata) { 52 void ui_condvar_signal(UiCondVar *var, void *data, int intdata) {
55 UiWinCondVar *p = (UiWinCondVar*)var; 53 UiWinCondVar *p = (UiWinCondVar*)var;
56 p->mutex.lock(); 54 std::unique_lock<std::mutex> lock(p->mutex);
57 p->var.data = data; 55 p->var.data = data;
58 p->var.intdata = intdata; 56 p->var.intdata = intdata;
59 p->set = 1; 57 p->set = 1;
58 lock.unlock();
60 p->cond.notify_one(); 59 p->cond.notify_one();
61 p->mutex.unlock();
62 } 60 }
63 61
64 void ui_condvar_destroy(UiCondVar *var) { 62 void ui_condvar_destroy(UiCondVar *var) {
65 UiWinCondVar *p = (UiWinCondVar*)var; 63 UiWinCondVar *p = (UiWinCondVar*)var;
66 free(p); 64 delete p;
67
68 } 65 }

mercurial