ptrigger.cxx (1394B)
1 /* 2 * 3 * C++ Portable Types Library (PTypes) 4 * Version 2.1.1 Released 27-Jun-2007 5 * 6 * Copyright (C) 2001-2007 Hovik Melikyan 7 * 8 * http://www.melikyan.com/ptypes/ 9 * 10 */ 11 12 #ifdef WIN32 13 # include <windows.h> 14 #else 15 # include <sys/time.h> 16 # include <pthread.h> 17 # include <errno.h> 18 #endif 19 20 #include "pasync.h" 21 22 23 namespace ptypes { 24 25 26 static void trig_fail() 27 { 28 fatal(CRIT_FIRST + 41, "Trigger failed"); 29 } 30 31 32 33 #ifdef WIN32 34 35 36 trigger::trigger(bool autoreset, bool state) 37 { 38 handle = CreateEvent(0, !autoreset, state, 0); 39 if (handle == 0) 40 trig_fail(); 41 } 42 43 44 #else 45 46 47 inline void trig_syscheck(int r) 48 { 49 if (r != 0) 50 trig_fail(); 51 } 52 53 54 trigger::trigger(bool iautoreset, bool istate) 55 : state(int(istate)), autoreset(iautoreset) 56 { 57 trig_syscheck(pthread_mutex_init(&mtx, 0)); 58 trig_syscheck(pthread_cond_init(&cond, 0)); 59 } 60 61 62 trigger::~trigger() 63 { 64 pthread_cond_destroy(&cond); 65 pthread_mutex_destroy(&mtx); 66 } 67 68 69 void trigger::wait() 70 { 71 pthread_mutex_lock(&mtx); 72 while (state == 0) 73 pthread_cond_wait(&cond, &mtx); 74 if (autoreset) 75 state = 0; 76 pthread_mutex_unlock(&mtx); 77 } 78 79 80 void trigger::post() 81 { 82 pthread_mutex_lock(&mtx); 83 state = 1; 84 if (autoreset) 85 pthread_cond_signal(&cond); 86 else 87 pthread_cond_broadcast(&cond); 88 pthread_mutex_unlock(&mtx); 89 } 90 91 92 void trigger::reset() 93 { 94 state = 0; 95 } 96 97 98 #endif 99 100 101 }