unix/TimeSupport.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41 
42 #include <pthread.h>
43 #if defined(OPSYS_SUN)
44 #include <sched.h>
45 #endif
46 #include "RexxCore.h"
47 #include "IntegerClass.hpp"
48 #include "RexxDateTime.hpp"
49 #include "SystemInterpreter.hpp"
50 
51 #ifdef AIX
52 #include <time.h>
53 #else
54 #include <sys/time.h>
55 #endif
56 
57 
59 {
60  struct tm *SystemDate; /* system date structure ptr */
61  struct timeval tv;
62  gettimeofday(&tv, NULL);
63 
64 #ifdef AIX
65  struct tm SD; /* system date area */
66  SystemDate = localtime_r((time_t *)&tv.tv_sec, &SD);
67 #else
68  SystemDate = localtime((time_t *)&tv.tv_sec); /* convert */
69 #endif
70 
71  Date->hours = SystemDate->tm_hour;
72  Date->minutes = SystemDate->tm_min;
73  Date->seconds = SystemDate->tm_sec;
74  Date->microseconds = tv.tv_usec;
75  Date->day = SystemDate->tm_mday;
76  Date->month = ++SystemDate->tm_mon;
77  Date->year = SystemDate->tm_year + 1900;
78 
79  struct tm *GMTDate; /* system date structure ptr */
80 #ifdef AIX
81  struct tm GD; /* system date area */
82  GMTDate = gmtime_r((time_t *)&tv.tv_sec, &GD);
83 #else
84  GMTDate = gmtime((time_t *)&tv.tv_sec);
85 #endif
86  // in microseconds
87  Date->timeZoneOffset = ((int64_t)(tv.tv_sec - mktime(GMTDate))) * 1000000UL;
88 }
89 
90 /*********************************************************************/
91 /* */
92 /* Subroutine Name: alarm_starTimer */
93 /* */
94 /* Function: starts an asynchronous, single_interval */
95 /* timer thread. When the timer pops, it will */
96 /* post an event semaphore. */
97 /* */
98 /* Arguments: alarm timer - time interval before the event */
99 /* semaphore is posted */
100 /*********************************************************************/
101 
102 RexxMethod2(int, alarm_startTimer,
103  wholenumber_t, numdays,
104  wholenumber_t, alarmtime)
105 {
106  SysSemaphore sem("alarm_startTimer : sem", true); /* Event-semaphore */
107  int msecInADay = 86400000; /* number of milliseconds in a day */
108 
109  /* set the state variables */
110  context->SetObjectVariable("EVENTSEMHANDLE", context->NewPointer(&sem));
111  context->SetObjectVariable("TIMERSTARTED", context->True());
112 
113  while (numdays > 0)
114  { /* is it some future day? */
115  // use the semaphore to wait for an entire day.
116  // if this returns true, then this was not a timeout, which
117  // probably means this was cancelled.
118  if (sem.wait("alarm_startTimer", 0, msecInADay))
119  {
120  /* Check if the alarm is canceled. */
121  RexxObjectPtr cancelObj = context->GetObjectVariable("CANCELED");
122 
123  if (cancelObj == context->True())
124  {
125  return 0;
126  }
127  else
128  {
129  sem.reset(); /* Reset the event semaphore */
130  }
131  }
132  numdays--; /* Decrement number of days */
133  }
134 
135  // now we can just wait for the alarm time to expire
136  sem.wait("alarm_startTimer", 0, alarmtime);
137  return 0;
138 }
139 
140 /*********************************************************************/
141 /* */
142 /* Subroutine Name: alarm_stopTimer */
143 /* */
144 /* Function: stops an asynchronous timer. */
145 /* */
146 /* Arguments: timer - timer handle */
147 /* eventSemHandle - event semaphore handle */
148 /* shared between start & stop timer */
149 /*********************************************************************/
150 
151 
152 RexxMethod1(int, alarm_stopTimer, POINTER, eventSemHandle)
153 {
154  SysSemaphore *sev = (SysSemaphore *)eventSemHandle; /* event semaphore handle */
155  sev->post(); /* Post the event semaphore */
156  return 0;
157 }
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
int64_t timeZoneOffset
void wait(const char *ds, int di)
static void getCurrentTime(RexxDateTime *Date)
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
ssize_t wholenumber_t
Definition: rexx.h:230
void * POINTER
Definition: rexx.h:79
RexxMethod1(int, alarm_stopTimer, POINTER, eventSemHandle)
RexxMethod2(int, alarm_startTimer, wholenumber_t, numdays, wholenumber_t, alarmtime)
signed __int64 int64_t