DSL
Mutex.cpp
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #include <drift/dslcore.h>
12 #include <drift/Mutex.h>
13 #include <drift/Threading.h>
14 
15 /*
16 DSL_MutexLocker::DSL_MutexLocker(DSL_Mutex_Base * mutex) {
17  hMutex = mutex;
18  LockMutexPtr(mutex);
19 }
20 DSL_MutexLocker::~DSL_MutexLocker() {
21  RelMutexPtr(hMutex);
22 }
23 */
24 
25 
26 /*
27 #if defined(WIN32)
28 DSL_MutexLocker::DSL_MutexLocker(DSL_Mutex_Win32Mutex * mutex, const char * fn, int line) {
29  hMutexCS = NULL;
30  hMutex = mutex;
31  LockMutexPtr(mutex);
32 }
33 DSL_MutexLocker::DSL_MutexLocker(DSL_Mutex_Win32CS * mutex, const char * fn, int line) {
34  hMutex = NULL;
35  hMutexCS = mutex;
36  LockMutexPtr(mutex);
37 }
38 DSL_MutexLocker::~DSL_MutexLocker() {
39  if (hMutex != NULL) {
40  RelMutexPtr(hMutex);
41  }
42  if (hMutexCS != NULL) {
43  RelMutexPtr(hMutexCS);
44  }
45 }
46 #else
47 */
48 //#endif
49 
50 /*
51 DSL_Mutex hMutexWatchdog;
52 bool bMutexWatchdogShutdown = false;
53 bool bMutexWatchdogRunning = false;
54 map<DSL_Mutex *, time_t> watchdog_list;
55 
56 DSL_DEFINE_THREAD(Mutex_Watchdog) {
57  DSL_THREAD_START
58  bMutexWatchdogRunning = true;
59  while (!bMutexWatchdogShutdown) {
60  hMutexWatchdog.Lock();
61  time_t tme = time(NULL);
62  for (auto x = watchdog_list.begin(); x != watchdog_list.begin(); x++) {
63  DSL_Mutex * hMutex = x->first;
64  if (hMutex->IsLocked()) {
65  time_t exp = hMutex->last_mod_time + x->second;
66  if (tme > exp) {
67  printf("DSL Watchdog: Unlocking mutex %p\n", hMutex);
68  hMutex->Release();
69  }
70  }
71  }
72  hMutexWatchdog.Release();
73 
74  safe_sleep(100, true);
75  }
76  bMutexWatchdogRunning = false;
77  DSL_THREAD_END
78 }
79 
80 bool DSL_CC dsl_mutex_watchdog_init();
81 void DSL_CC dsl_mutex_watchdog_add(DSL_Mutex * hMutex, time_t timeout);
82 void DSL_CC dsl_mutex_watchdog_remove(DSL_Mutex * hMutex);
83 void DSL_CC dsl_mutex_watchdog_quit();
84 
85 bool DSL_CC dsl_mutex_watchdog_init() {
86  if (DSL_StartThread(Mutex_Watchdog, NULL, "Mutex Watchdog") != NULL) {
87  bMutexWatchdogRunning = true;
88  }
89 }
90 void DSL_CC dsl_mutex_watchdog_add(DSL_Mutex * hMutex, time_t timeout) {
91  AutoMutex(hMutexWatchdog);
92  watchdog_list[hMutex] = timeout;
93 }
94 void DSL_CC dsl_mutex_watchdog_remove(DSL_Mutex * hMutex) {
95  AutoMutex(hMutexWatchdog);
96  auto x = watchdog_list.find(hMutex);
97  if (x != watchdog_list.end()) {
98  watchdog_list.erase(x);
99  }
100 }
101 void DSL_CC dsl_mutex_watchdog_quit() {
102  bMutexWatchdogShutdown = true;
103  while (bMutexWatchdogRunning) {
104  safe_sleep(100, true);
105  }
106 }
107 */