1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #define SEM_NAME1 "/tmp/foo.sem"
12 #define SEM_NAME2 "/tmp/bar.sem"
14 #define ITERATIONS 1000
16 static PRBool debug_mode
= PR_FALSE
;
17 static PRIntn iterations
= ITERATIONS
;
18 static PRIntn counter
;
19 static PRSem
*sem1
, *sem2
;
22 * Thread 2 waits on semaphore 2 and posts to semaphore 1.
24 void ThreadFunc(void *arg
)
28 for (i
= 0; i
< iterations
; i
++) {
29 if (PR_WaitSemaphore(sem2
) == PR_FAILURE
) {
30 fprintf(stderr
, "PR_WaitSemaphore failed\n");
33 if (counter
== 2*i
+1) {
34 if (debug_mode
) printf("thread 2: counter = %d\n", counter
);
36 fprintf(stderr
, "thread 2: counter should be %d but is %d\n",
41 if (PR_PostSemaphore(sem1
) == PR_FAILURE
) {
42 fprintf(stderr
, "PR_PostSemaphore failed\n");
48 static void Help(void)
50 fprintf(stderr
, "sema test program usage:\n");
51 fprintf(stderr
, "\t-d debug mode (FALSE)\n");
52 fprintf(stderr
, "\t-c <count> loop count (%d)\n", ITERATIONS
);
53 fprintf(stderr
, "\t-h this message\n");
56 int main(int argc
, char **argv
)
61 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dc:h");
63 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
))) {
64 if (PL_OPT_BAD
== os
) continue;
65 switch (opt
->option
) {
66 case 'd': /* debug mode */
69 case 'c': /* loop count */
70 iterations
= atoi(opt
->value
);
78 PL_DestroyOptState(opt
);
80 if (PR_DeleteSemaphore(SEM_NAME1
) == PR_SUCCESS
) {
81 fprintf(stderr
, "warning: removed semaphore %s left over "
82 "from previous run\n", SEM_NAME1
);
84 if (PR_DeleteSemaphore(SEM_NAME2
) == PR_SUCCESS
) {
85 fprintf(stderr
, "warning: removed semaphore %s left over "
86 "from previous run\n", SEM_NAME2
);
89 sem1
= PR_OpenSemaphore(SEM_NAME1
, PR_SEM_CREATE
, SEM_MODE
, 1);
91 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
92 PR_GetError(), PR_GetOSError());
95 sem2
= PR_OpenSemaphore(SEM_NAME2
, PR_SEM_CREATE
, SEM_MODE
, 0);
97 fprintf(stderr
, "PR_OpenSemaphore failed\n");
100 thred
= PR_CreateThread(PR_USER_THREAD
, ThreadFunc
, NULL
,
101 PR_PRIORITY_NORMAL
, PR_GLOBAL_THREAD
, PR_JOINABLE_THREAD
, 0);
103 fprintf(stderr
, "PR_CreateThread failed\n");
108 * Thread 1 waits on semaphore 1 and posts to semaphore 2.
110 for (i
= 0; i
< iterations
; i
++) {
111 if (PR_WaitSemaphore(sem1
) == PR_FAILURE
) {
112 fprintf(stderr
, "PR_WaitSemaphore failed\n");
115 if (counter
== 2*i
) {
116 if (debug_mode
) printf("thread 1: counter = %d\n", counter
);
118 fprintf(stderr
, "thread 1: counter should be %d but is %d\n",
123 if (PR_PostSemaphore(sem2
) == PR_FAILURE
) {
124 fprintf(stderr
, "PR_PostSemaphore failed\n");
129 if (PR_JoinThread(thred
) == PR_FAILURE
) {
130 fprintf(stderr
, "PR_JoinThread failed\n");
134 if (PR_CloseSemaphore(sem1
) == PR_FAILURE
) {
135 fprintf(stderr
, "PR_CloseSemaphore failed\n");
137 if (PR_CloseSemaphore(sem2
) == PR_FAILURE
) {
138 fprintf(stderr
, "PR_CloseSemaphore failed\n");
140 if (PR_DeleteSemaphore(SEM_NAME1
) == PR_FAILURE
) {
141 fprintf(stderr
, "PR_DeleteSemaphore failed\n");
143 if (PR_DeleteSemaphore(SEM_NAME2
) == PR_FAILURE
) {
144 fprintf(stderr
, "PR_DeleteSemaphore failed\n");