Debian Codegraph @

frmts/pcidsk/sdk/port/pthread_mutex.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Purpose: Implementation of pthreads based mutex.
4 *
5 ******************************************************************************
6 * Copyright (c) 2009
7 * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada.
8 *
9 * SPDX-License-Identifier: MIT
10 ****************************************************************************/
11
12 #include "pcidsk_mutex.h"
13 #include <pthread.h>
14
15 #include <cstdlib>
16
17
18 /************************************************************************/
19 /* PThreadMutex */
20 /************************************************************************/
21
22 class PThreadMutex final: public PCIDSK::Mutex
23
24 {
25 private:
26 pthread_mutex_t *hMutex;
27
28 PThreadMutex(const PThreadMutex&) = delete;
29 PThreadMutex& operator=(const PThreadMutex&) = delete;
30
31 public:
32 PThreadMutex();
33 ~PThreadMutex();
34
35 int Acquire(void) override;
36 int Release(void) override;
37 };
38
39 /************************************************************************/
40 /* PThreadMutex() */
41 /************************************************************************/
42
43 PThreadMutex::PThreadMutex()
44
45 {
46 hMutex = static_cast<pthread_mutex_t *>(malloc(sizeof(pthread_mutex_t)));
47
48 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
49 pthread_mutex_t tmp_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
50 *hMutex = tmp_mutex;
51 #elif defined(PTHREAD_MUTEX_RECURSIVE) || defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
52 {
53 pthread_mutexattr_t attr;
54 pthread_mutexattr_init( &attr );
55 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
56 pthread_mutex_init( hMutex, &attr );
57 }
58 #else
59 #error "Recursive mutexes apparently unsupported, configure --without-threads"
60 #endif
61 }
62
63 /************************************************************************/
64 /* ~PThreadMutex() */
65 /************************************************************************/
66
67 PThreadMutex::~PThreadMutex()
68
69 {
70 pthread_mutex_destroy( hMutex );
71 free( hMutex );
72 }
73
74 /************************************************************************/
75 /* Release() */
76 /************************************************************************/
77
78 int PThreadMutex::Release()
79
80 {
81 pthread_mutex_unlock( hMutex );
82 return 1;
83 }
84
85 /************************************************************************/
86 /* Acquire() */
87 /************************************************************************/
88
89 int PThreadMutex::Acquire()
90
91 {
92 return pthread_mutex_lock( hMutex ) == 0;
93 }
94
95 /************************************************************************/
96 /* DefaultCreateMutex() */
97 /************************************************************************/
98
99 /**
100 * Create a mutex.
101 *
102 * This function creates the default style of mutex for the currently
103 * PCIDSK library build. The mutex should be destroyed with delete when
104 * no longer required.
105 *
106 * @return a new mutex object pointer.
107 */
108
109
110 PCIDSK::Mutex *PCIDSK::DefaultCreateMutex(void)
111
112 {
113 return new PThreadMutex();
114 }