blob: 595dd46af6ea831ea388e780ed6078947635929b [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39#include <cc++/config.h>
40#include <cc++/export.h>
41#include <cc++/thread.h>
42#include "private.h"
43
44#include <cstdlib>
45
46#ifndef WIN32
47
48#include <sys/time.h>
49#ifdef SIGTSTP
50#include <sys/file.h>
51#include <sys/ioctl.h>
52#endif
53
54#ifndef _PATH_TTY
55#define _PATH_TTY "/dev/tty"
56#endif
57
58#endif // !WIN32
59
60#ifdef CCXX_NAMESPACES
61namespace ost {
62#endif
63
64#ifndef WIN32
65
66timespec *getTimeout(struct timespec *spec, timeout_t timer)
67{
68 static struct timespec myspec;
69
70 if(spec == NULL)
71 spec = &myspec;
72
73#ifdef PTHREAD_GET_EXPIRATION_NP
74 struct timespec offset;
75
76 offset.tv_sec = timer / 1000;
77 offset.tv_nsec = (timer % 1000) * 1000000;
78 pthread_get_expiration_np(&offset, spec);
79#else
80 struct timeval current;
81
82 SysTime::getTimeOfDay(&current);
83 spec->tv_sec = current.tv_sec + ((timer + current.tv_usec / 1000) / 1000);
84 spec->tv_nsec = ((current.tv_usec / 1000 + timer) % 1000) * 1000000;
85
86#endif
87 return spec;
88}
89
90#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
91void wait(signo_t signo)
92{
93 sigset_t mask;
94
95 sigemptyset(&mask);
96 sigaddset(&mask, signo);
97#ifdef HAVE_SIGWAIT2
98 sigwait(&mask, &signo);
99#else
100 sigwait(&mask);
101#endif
102}
103#endif
104
105/*
106void Thread::yield(void)
107{
108#ifdef HAVE_PTHREAD_YIELD
109 pthread_yield();
110#endif
111}
112*/
113
114#ifdef CCXX_SIG_THREAD_CANCEL
115
116#if defined(HAVE_PTHREAD_NANOSLEEP) || defined(HAVE_PTHREAD_DELAY)
117void Thread::sleep(timeout_t timeout)
118{
119 struct timespec ts;
120 Cancel old = Thread::enterCancel();
121
122 ts.tv_sec = timeout / 1000;
123 ts.tv_nsec = (timeout % 1000) * 1000000000;
124#ifdef HAVE_PTHREAD_DELAY
125 pthread_delay(&ts);
126#else
127 nanosleep(&ts);
128#endif
129 Thread::exitCancel(old);
130}
131#else
132void Thread::sleep(timeout_t timeout)
133{
134 Cancel old = Thread::enterCancel();
135 usleep(timeout * 1000);
136 Thread::exitCancel(old);
137}
138#endif
139
140#else
141
142#if defined(HAVE_PTHREAD_DELAY) || defined(HAVE_PTHREAD_NANOSLEEP)
143
144void Thread::sleep(timeout_t timeout)
145{
146 struct timespec timer;
147 timer.tv_sec = timeout / 1000;
148 timer.tv_nsec = (timeout % 1000) * 1000000;
149
150#ifdef HAVE_PTHREAD_DELAY
151 pthread_delay(&timer);
152#else
153 nanosleep(&timer);
154#endif
155}
156
157#else
158
159void Thread::sleep(timeout_t timeout)
160{
161 usleep(timeout * 1000);
162}
163#endif
164
165#endif
166
167#endif // !WIN32
168
169#ifdef CCXX_NAMESPACES
170}
171#endif
172
173/** EMACS **
174 * Local variables:
175 * mode: c++
176 * c-basic-offset: 4
177 * End:
178 */