-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadsync.c
More file actions
73 lines (59 loc) · 2.52 KB
/
threadsync.c
File metadata and controls
73 lines (59 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/******************************************************************************
File: threadsync.c
Created: 2019-07-25
Updated: 2019-08-01
Author: Aaron Oman
Notice: Creative Commons Attribution 4.0 International License (CC-BY 4.0)
******************************************************************************/
//! \file threadsync.c
//! Cross-thread signalling
struct thread_sync {
pthread_rwlock_t lock;
int shouldShutdown; //!< Signal to trigger graceful thread shutdown
};
//! \brief Creates and initializes a new thread_sync object instance
//! \return The initialized thread_sync object
struct thread_sync *ThreadSyncInit() {
struct thread_sync *s = (struct thread_sync *)malloc(sizeof(struct thread_sync));
s->shouldShutdown = 0;
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_setpshared(&attr, 1);
if (0 != pthread_rwlock_init(&s->lock, &attr)) {
fprintf(stderr, "Couldn't initialize thread_sync rwlock");
return NULL;
}
return s;
}
//! \brief De-initializes and frees memory for the given thread_sync object
//! \param[in,out] threadSync The initialized thread_sync object to be cleaned and reclaimed
void ThreadSyncDeinit(struct thread_sync *threadSync) {
if (NULL == threadSync) return;
if (0 != pthread_rwlock_destroy(&threadSync->lock)) {
fprintf(stderr, "Couldn't destroy thread_Sync lock");
}
free(threadSync);
}
//! \brief atomically updates shouldShutdown status of thread_sync state
//! \param[in,out] threadSync thread_sync state to read
//! \return 1 when threadSync has been triggered, otherwise 0
int ThreadSyncShouldShutdown(struct thread_sync *threadSync) {
if (0 != pthread_rwlock_rdlock(&threadSync->lock)) {
fprintf(stderr, "Failed to lock thread_sync rwlock");
return 0;
}
int result = threadSync->shouldShutdown;
pthread_rwlock_unlock(&threadSync->lock);
return result;
}
//! \brief atomically updates shouldShutdown status of thread_sync state
//! \param[in,outv threadSync thread_sync state to update
void ThreadSyncSignalShutdown(struct thread_sync *threadSync) {
if (NULL == threadSync) return;
if (0 != pthread_rwlock_wrlock(&threadSync->lock)) {
fprintf(stderr, "Failed to lock thread_sync rwlock");
return;
}
threadSync->shouldShutdown = 1;
pthread_rwlock_unlock(&threadSync->lock);
}