blob: d3a3db6d91faf67a4f2fb214acdfdcce95efb64c (
plain)
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
|
#include "unity.h"
#include <pthread.h>
#include <sys/delay.h>
#define NUM_THREADS 2
static pthread_t threads[NUM_THREADS];
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static int execs;
static void init() {
int ret;
ret = pthread_mutex_lock(&mutex);
TEST_ASSERT(ret == 0);
execs++;
ret = pthread_mutex_unlock(&mutex);
TEST_ASSERT(ret == 0);
}
static void *thread1(void *args) {
int ret;
ret = pthread_once(&once, init);
TEST_ASSERT(ret == 0);
// Simulate some work
usleep(1000);
pthread_exit(NULL);
}
TEST_CASE("pthread once", "[pthread]") {
pthread_attr_t attr;
int i, ret;
ret = pthread_attr_init(&attr);
TEST_ASSERT(ret == 0);
ret = pthread_mutex_init(&mutex, NULL);
TEST_ASSERT(ret == 0);
ret = pthread_create(&threads[0], &attr, thread1, NULL);
TEST_ASSERT(ret == 0);
ret = pthread_create(&threads[1], &attr, thread1, NULL);
TEST_ASSERT(ret == 0);
// Wait for all threads completion
for (i=0; i< NUM_THREADS; i++) {
ret = pthread_join(threads[i], NULL);
TEST_ASSERT(ret == 0);
}
TEST_ASSERT(execs == 1);
// Clean up
ret = pthread_attr_destroy(&attr);
TEST_ASSERT(ret == 0);
ret = pthread_mutex_destroy(&mutex);
TEST_ASSERT(ret == 0);
}
|