1 /** @file
2
3 A brief file description
4
5 @section license License
6
7 Licensed to the Apache Software Foundation (ASF) under one
8 or more contributor license agreements. See the NOTICE file
9 distributed with this work for additional information
10 regarding copyright ownership. The ASF licenses this file
11 to you under the Apache License, Version 2.0 (the
12 "License"); you may not use this file except in compliance
13 with the License. You may obtain a copy of the License at
14
15 http://www.apache.org/licenses/LICENSE-2.0
16
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
23
24 /**************************************************************************
25
26 ink_thread.cc
27
28 Generic threads interface.
29 **************************************************************************/
30
31 #include "tscore/ink_platform.h"
32 #include "tscore/ink_thread.h"
33 #include "tscore/ink_atomic.h"
34
35 // // ignore the compiler warning... so that this can be used
36 // // in the face of changes to the Solaris header files (see "man thread")
37 //
38 // ink_mutex ink_mutex_initializer = INK_MUTEX_INIT;
39 //
40 // Put a bracketed initializer in ink_thread.h --- 9/19/96, bri
41 ink_mutex ink_mutex_initializer = INK_MUTEX_INIT;
42
43 #if TS_EMULATE_ANON_SEMAPHORES
44 static int64_t ink_semaphore_count = 0;
45 #endif
46
47 void
ink_sem_init(ink_semaphore * sp,unsigned int count)48 ink_sem_init(ink_semaphore *sp, unsigned int count)
49 {
50 // Darwin has sem_open, but not sem_init. We emulate sem_init with sem_open.
51 #if TS_EMULATE_ANON_SEMAPHORES
52 char sname[NAME_MAX];
53
54 sp->semid = ink_atomic_increment(&ink_semaphore_count, 1);
55 snprintf(sname, sizeof(sname), "/trafficserver/anon/%" PRId64, sp->semid);
56
57 ink_assert((sp->sema = sem_open(sname, O_CREAT | O_EXCL, 0770, count)) != SEM_FAILED);
58
59 // Since we are emulating anonymous semaphores, unlink the name
60 // so no other process can accidentally get it.
61 ink_assert(sem_unlink(sname) != -1);
62 #else
63 ink_assert(sem_init(sp->get(), 0 /* pshared */, count) != -1);
64 #endif
65 }
66
67 void
ink_sem_destroy(ink_semaphore * sp)68 ink_sem_destroy(ink_semaphore *sp)
69 {
70 #if TS_EMULATE_ANON_SEMAPHORES
71 ink_assert(sem_close(sp->get()) != -1);
72 #else
73 ink_assert(sem_destroy(sp->get()) != -1);
74 #endif
75 }
76
77 void
ink_sem_wait(ink_semaphore * sp)78 ink_sem_wait(ink_semaphore *sp)
79 {
80 int r;
81 while (EINTR == (r = sem_wait(sp->get()))) {
82 ;
83 }
84 ink_assert(!r);
85 }
86
87 bool
ink_sem_trywait(ink_semaphore * sp)88 ink_sem_trywait(ink_semaphore *sp)
89 {
90 int r;
91 while (EINTR == (r = sem_trywait(sp->get()))) {
92 ;
93 }
94 ink_assert(r == 0 || (errno == EAGAIN));
95 return r == 0;
96 }
97
98 void
ink_sem_post(ink_semaphore * sp)99 ink_sem_post(ink_semaphore *sp)
100 {
101 ink_assert(sem_post(sp->get()) != -1);
102 }
103