• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_thread.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /**************************************************************************
00025 
00026   ink_thread.cc
00027 
00028   Generic threads interface.
00029 **************************************************************************/
00030 
00031 #include "libts.h"
00032 
00033 // // ignore the compiler warning... so that this can be used
00034 // // in the face of changes to the Solaris header files (see "man thread")
00035 //
00036 // ink_mutex ink_mutex_initializer = INK_MUTEX_INIT;
00037 //
00038 // Put a bracketed initializer in ink_thread.h --- 9/19/96, bri
00039 ink_mutex ink_mutex_initializer = INK_MUTEX_INIT;
00040 
00041 #if TS_EMULATE_ANON_SEMAPHORES
00042 static int64_t ink_semaphore_count = 0;
00043 #endif
00044 
00045 void
00046 ink_sem_init(ink_semaphore * sp, unsigned int count)
00047 {
00048   // Darwin has sem_open, but not sem_init. We emulate sem_init with sem_open.
00049 #if TS_EMULATE_ANON_SEMAPHORES
00050   char sname[NAME_MAX];
00051 
00052   sp->semid = ink_atomic_increment(&ink_semaphore_count, 1);
00053   snprintf(sname, sizeof(sname), "/trafficserver/anon/%" PRId64, sp->semid);
00054 
00055   ink_assert((sp->sema = sem_open(sname, O_CREAT | O_EXCL, 0770, count)) != SEM_FAILED);
00056 
00057   // Since we are emulating anonymous semaphores, unlink the name
00058   // so no other process can accidentally get it.
00059   ink_assert(sem_unlink(sname) != -1);
00060 #else
00061   ink_assert(sem_init(sp->get(), 0 /* pshared */, count) != -1);
00062 #endif
00063 }
00064 
00065 void
00066 ink_sem_destroy(ink_semaphore * sp)
00067 {
00068 #if TS_EMULATE_ANON_SEMAPHORES
00069   ink_assert(sem_close(sp->get()) != -1);
00070 #else
00071   ink_assert(sem_destroy(sp->get()) != -1);
00072 #endif
00073 }
00074 
00075 void
00076 ink_sem_wait(ink_semaphore * sp)
00077 {
00078   int r;
00079   while (EINTR == (r = sem_wait(sp->get())));
00080   ink_assert(!r);
00081 }
00082 
00083 bool
00084 ink_sem_trywait(ink_semaphore * sp)
00085 {
00086   int r;
00087   while (EINTR == (r = sem_trywait(sp->get())));
00088   ink_assert(r == 0 || (errno == EAGAIN));
00089   return r == 0;
00090 }
00091 
00092 void
00093 ink_sem_post(ink_semaphore * sp)
00094 {
00095   ink_assert(sem_post(sp->get()) != -1);
00096 }

Generated by  doxygen 1.7.1