summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/mpeg_shared/routine.cpp
blob: 546b8bc5b3cfd8fd4f2c30b7959cf15303a8535f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* $Id$ */

/* Copyright (c) 1995 Oregon Graduate Institute of Science and Technology
 * P.O.Box 91000-1000, Portland, OR 97291, USA;
 * 
 * Permission to use, copy, modify, distribute, and sell this software and its 
 * documentation for any purpose is hereby granted without fee, provided that 
 * the above copyright notice appear in all copies and that both that 
 * copyright notice and this permission notice appear in supporting 
 * documentation, and that the name of O.G.I. not be used in advertising or 
 * publicity pertaining to distribution of the software without specific, 
 * written prior permission.  O.G.I. makes no representations about the 
 * suitability of this software for any purpose.  It is provided "as is" 
 * without express or implied warranty.
 * 
 * O.G.I. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 
 * O.G.I. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Shanwei Cen
 *         Department of Computer Science and Engineering
 *         email: scen@cse.ogi.edu
 */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#ifdef __svr4__
#include <sys/systeminfo.h>
#endif
#ifdef _HPUX_SOURCE
#include <sys/rtprio.h>
#elif  defined(__svr4__)
#include <sys/priocntl.h>
#include <sys/rtpriocntl.h>
#include <sys/tspriocntl.h>
#endif

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/sem.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <string.h>
#include "routine.h"

ACE_RCSID(mpeg_shared, routine, "$Id$")

#define TEST_TYPE 1
#define SIGSETMASK 0
#define stacksize 10000
#define TIME_ROUND 4000
#define BLOCK_NUM 5
#define DELTA_MSEC 20

int get_hostname(char *name, int len)
{
#ifdef __svr4__
  if (sysinfo(SI_HOSTNAME, name, len) == -1) return -1;
  else return 0;
#else
  return ACE_OS::hostname(name, len);
#endif
}

long get_usec(void)
{
  struct timeval tp;
  struct timezone tzp;
  int err;
  if (err=gettimeofday(&tp, &tzp))
  {
    return 0;
  }
  else {
    /*
    return ((long)1000000)*(tp.tv_sec%TIME_ROUND) + (tp.tv_usec);
    */
    return ((long)1000000)*(tp.tv_sec) + (tp.tv_usec);
  }
}

long get_msec(void)
{
  struct timeval tp;
  struct timezone tzp;
  int err;
  if (err=gettimeofday(&tp, &tzp))
  {
    return 0;
  }
  else
  {
    return ((long)1000)*(tp.tv_sec) + (tp.tv_usec / 1000);
  }
}

long get_sec(void)
{
  struct timeval tp;
  struct timezone tzp;
  int err;
  if (err=gettimeofday(&tp, &tzp))
  {
    return 0;
  }
  else
  {
    return tp.tv_sec;
  }
}

/* returns the difference of the two usec time values */

long get_duration(long val1, long val2)
{
  /*
  return val2>=val1 ? val2-val1 : (val2+TIME_ROUND-val1);
  */
  return val2>=val1 ? val2-val1 : ((LONG_MAX - val1) + (val2 - LONG_MIN) + 1);
}

#ifdef LINUX
#else
void usleep(unsigned int usec)
{
  struct timeval val;

  if (usec <= 0) return;
  val.tv_sec = usec / 1000000;
  val.tv_usec = usec % 1000000;
  if (select(0, NULL, NULL, NULL, &val) == -1 && errno != 4)
  {
    perror("sleep with select");
    exit(1);
  }
}
#endif

void beep(void)
{
  fprintf(stderr, "\007");
}

#ifndef FreeBSD
#include <malloc.h>
#endif
#include <sys/shm.h>

void remove_shmem_id(int id)
{
  shmctl(id, IPC_RMID, NULL);
#if 0
  /* weird also on some sunOs4 machines */
  /* weird, HPUX always report error on this operation */
  if (errno)
  {
    fprintf(stderr, "shmctl shm_id:%d to IPC_RMID error:", id);
    perror("");
  }
#endif
}

char * creat_shared_mem_id(int size, int * id)
{
  char * shm;
  int shm_id;
  
  shm_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0666);
  if (shm_id == -1)
  {
    shm_id = shmget(IPC_PRIVATE, 0, IPC_CREAT | 0666);
    if (shm_id == -1)
    {
      perror("Shared MEM shmget1 error");
      exit(1);
    }
    else
    {
      if (shmctl(shm_id, IPC_RMID, NULL) == -1)
      {
        perror("Shared MEM shmctl error");
        exit(1);
      }
      shm_id = shmget(IPC_PRIVATE, 0, IPC_CREAT | 0666);
      if (shm_id == -1)
      {
        perror("Shared MEM shmget2 error");
        exit(1);
      }
    }
  }
  shm = (char *)shmat(shm_id, (char *)0, 0);
  if ((int)shm == -1)
  {
    perror("Shared MEM shmat error");
    exit(1);
  }
  *id = shm_id;
  return shm;
}

char * creat_shared_mem(int size)
{
  int shm_id;
  char * shm = creat_shared_mem_id(size, &shm_id);
  remove_shmem_id(shm_id);
  return shm;
}

void remove_shared_mem(char *ptr)
{
  shmdt(ptr);
}

#define SEM_NUM 6
static int semId = -1;
static int masterPid = -1;
static int nextSem = -1;

void enter_cs(int semaphore_id)
{
  struct sembuf sop;
  sop.sem_num = semaphore_id;
  sop.sem_op = -1;
  sop.sem_flg = 0;
  while (semop(semId, &sop, 1) == -1)
  {
    if (errno == EINTR)
      continue;
    fprintf(stderr, "semop(enter_cs) error: pid=%d", getpid());
    perror("");
    exit(1);
  }
}

void leave_cs(int semaphore_id)
{
  struct sembuf sop;
  sop.sem_num = semaphore_id;
  sop.sem_op = 1;
  sop.sem_flg = 0;
  while (semop(semId, &sop, 1) == -1)
  {
    if (errno == EINTR)
      continue;
    fprintf(stderr, "semop(leave_cs) error, pid=%d", getpid());
    perror("");
    exit(1);
  }
}

int creat_semaphore(void)
{
  int i;
  int sem_val;
  int semaphore_id;
  if (semId == -1) {
    semId = semget(IPC_PRIVATE, SEM_NUM, IPC_CREAT | 0666);
    if (semId == -1) {
      perror("Semaphore semget error");
      exit(1);
    }
    masterPid = getpid();
    nextSem = 0;
  }
  else if (masterPid != getpid()) {
    fprintf(stderr, "Error: this creat_semaphore() assumes semaphores are allocated\n");
    fprintf(stderr, "  only in single process %d, while current pid=%d\n",
	    masterPid, getpid());
    exit(1);
  }
  semaphore_id = nextSem ++;
  if (semaphore_id >= SEM_NUM) {
    fprintf(stderr, "Error: all of %d semaphores used up.\n", SEM_NUM);
    exit(1);
  }
  sem_val = semctl(semId, semaphore_id, GETVAL, 0);
/*
   fprintf(stderr, "Initial semaphore value: %d\n", sem_val);
*/
  if (sem_val == 0)
    leave_cs(semaphore_id);
  return semaphore_id;
}

void delete_semaphore()
{
  if (masterPid == getpid() && semId >= 0) {
    semctl(semId, 0, IPC_RMID, 0);
  } else {
    if (masterPid != getpid())
    fprintf(stderr, "Pid %d not supposed to remove semId created by pid %d\n",
	    getpid(), masterPid);
    else
    fprintf(stderr, "The semaphore has been deleted.\n");
  }
  return;
}

void remove_semaphore(int sid)
{
  return;
  //  semctl(semId, semaphore_id, IPC_RMID, 0);
}

void remove_all_semaphores (void)
{
  semctl(semId,0, IPC_RMID, 0);
  return;
}

int get_semval(int sid)
{
  int val, val1 = 0;
  errno = 0;
  val = semctl(semId, sid, GETVAL, 0);
  if (val == -1) {
    perror("getting value of a semaphore");
    exit(1);
  }
  /*
  if (val == 0) {
    fprintf(stderr, "pid %d to call semctl(%d, 0, GETZCNT)\n", getpid(), sid);
    val = semctl(semId, 0, GETZCNT, 0);
    if (val == -1) {
      perror("getting semzcnt of a semaphore");
      exit(1);
    }
    
    fprintf(stderr, "pid %d to call semctl(%d, 0, GETNCNT)\n", getpid(), sid);
    usleep(10000000);
    val1 = semctl(semId, sid, GETNCNT, 0);
    if (val1 == -1) {
      perror("getting semncnt of a semaphore");
    }
    fprintf(stderr, "pid %d to called semctl(GETNCNT)\n", getpid());
    
    fprintf(stderr, "Semval val %d, val1 %d\n", val, val1);
    return (0-(val + val1));
  }
  else
  */
  return val;
}

void get_full_path(char *filename, char * buffer, int bufsize)
{
  char path[MAXPATHLEN];

  path[0] = 0;
  
  if (*filename == '/')
    strncpy(path, filename, MAXPATHLEN);
  else if (*filename == '~')
  {
    char * env = getenv("HOME");
    if (env != NULL)
    {
      strncpy(path, env, MAXPATHLEN);
      strncat(path, filename+1, MAXPATHLEN - strlen(path));
    }
    else
      strncpy(path, filename, MAXPATHLEN);
  }
  else  /* from current dir */
  {
    getcwd(path, MAXPATHLEN);
    if (errno)
      strncpy(path, filename, MAXPATHLEN);
    else
    {
      path[MAXPATHLEN-4] = 0;
      strcat(path, "/");
      strncat(path, filename, MAXPATHLEN - strlen(path));
    }
  }
  path[MAXPATHLEN-1] = 0;
  strncpy(buffer, path, bufsize);
  buffer[bufsize-1] = 0;
  /*
  fprintf(stderr, "%s extended to %s\n", filename, buffer);
  */
}

void setsignal(int sig, void (func)(int))
{
#ifdef _HPUX_SOURCE
  struct sigvec sv;

  sv.sv_handler = func;
  sv.sv_mask    = 0;
  sv.sv_flags   = 0;
  if (sigvector (sig, &sv, (struct sigvec *) NULL) == -1) {
    fprintf(stderr, "sigvector(%d) error", sig);
    perror("");
    exit(1);
  }
#elif defined(__svr4__) || defined(IRIX)
  if (sigset(sig, func) == SIG_ERR)  {
    fprintf(stderr, "sigset(%d,func) error", sig);
    perror("");
    exit(1);
  }
#elif defined(sun) || defined(FreeBSD) || defined(ULTRIX) || defined(LINUX)
  {
    struct sigaction act;
    act.sa_handler = func;
#ifdef FreeBSD
    act.sa_flags = SV_INTERRUPT;
#else
    act.sa_flags =/* SA_INTERRUPT;*/ SA_RESTART;
#endif
    // @@ Naga commented this line.
    //    act.sa_mask = 0;
    if (!sigaction(sig, &act, NULL)) return;
    fprintf(stderr, "sigaction(%d,...) error", sig);
    perror("");
    exit(1);
  }
  /*
  if (signal(sig, func) == SIG_ERR) {
    fprintf(stderr, "signal(%d,func) error", sig);
    perror("");
    exit(1);
  }
  */
#else
  fprintf(stderr,
	  "Error: code for setsignal(%d,func) is missing in source/mpeg_shared/routine.cpp\n",
	  sig);
  exit(1);
#endif
}

/* set to 'pri': 0 -lowest RT, higher value higher RT priority. -1 - Time Sharing.
   For HPUX, 0 -> 127, 1 -> 127, ..., pri -> 127 - pri;
   For Solaris, -> 0, 1 -> 1, pri -> pri;

   The function returns 0 on success, -1 on failure
   */
int SetRTpriority(char *msg, int pri)
{
  if (pri >= 0) {
#ifdef _HPUX_SOURCE
    if (rtprio(0, 127 - pri) == -1)
    {
      fprintf(stderr, "%s fails to be set to RT priority %d", msg, 127 - pri);
      perror("");
      return -1;
    }
    return 0;
#elif defined(__svr4__)
    pcinfo_t pci;
    pcparms_t pcp;
    rtparms_t * rtp = (rtparms_t *)pcp.pc_clparms;
    strcpy(pci.pc_clname, "RT");
    if (priocntl(P_PID, P_MYID, PC_GETCID, (char *)&pci) == -1) {
      fprintf(stderr, "%s priocntl(PC_GETCID) failed for RT pri %d", msg, pri);
      perror("");
      return -1;
    }
    pcp.pc_cid = pci.pc_cid;
    rtp->rt_pri = pri;
    rtp->rt_tqsecs = 0;
    rtp->rt_tqnsecs = 10000000;  /* 10 (ten) milliseconds */
    if (priocntl(P_PID, P_MYID, PC_SETPARMS, (char *)&pcp) == -1) {
      fprintf(stderr, "%s priocntl(PC_SETPARMS) failed for RT pri %d", msg, pri);
      perror("");
      return -1;
    }
    return 0;
#else
    fprintf(stderr, "%s warning: RT priority not available on this arch.\n", msg);
    return -1;
#endif
  }
  else {  /* set to TS priority */
#ifdef _HPUX_SOURCE
    if (rtprio(0, RTPRIO_RTOFF) == -1) {
      fprintf(stderr, "%s fails to be set to RTPRIO_RTOFF", msg);
      perror("");
      return -1;
    }
    return 0;
#elif defined(__svr4__)
    pcinfo_t pci;
    pcparms_t pcp;
    tsparms_t * tsp = (tsparms_t *)pcp.pc_clparms;
    strcpy(pci.pc_clname, "TS");
    if (priocntl(P_PID, P_MYID, PC_GETCID, (char *)&pci) == -1) {
      fprintf(stderr, "%s priocntl(PC_GETCID) failed for TS priority", msg);
      perror("");
      return -1;
    }
    pcp.pc_cid = pci.pc_cid;
    tsp->ts_uprilim = 0;
    tsp->ts_upri = 0;
    if (priocntl(P_PID, P_MYID, PC_SETPARMS, (char *)&pcp) == -1) {
      fprintf(stderr, "%s priocntl(PC_SETPARMS) failed for TS priority", msg);
      perror("");
      return -1;
    }
    return 0;
#endif
  }
}