Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Data description]
[simgrid.git] / src / gras / Common / gras.c
1 /* $Id$ */
2
3 /* gras.c - common parts for the Grid Reality And Simulation                */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "gras_private.h"
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /* Lock handling                                                          */
16 /**************************************************************************/
17 /**************************************************************************/
18
19 #ifdef HAVE_LIBPTHREAD
20 #include <pthread.h>
21
22 /* this is the lock for this module. Every other module will use a void *
23  * as a variable as mutex. */
24 pthread_mutex_t gras_thelock = PTHREAD_MUTEX_INITIALIZER;
25 #endif
26
27 int
28 gras_lock() {
29   //fprintf(stderr,"Get Lock... ");
30 #ifdef HAVE_LIBPTHREAD
31   int ret;
32   
33   ret = pthread_mutex_lock((pthread_mutex_t *)(&gras_thelock));
34   if  (ret != 0) {
35     fprintf(stderr, "gras_lock: Unable to lock (errno = %d)!\n", ret);
36     return 0;
37   }
38 #endif
39   //fprintf(stderr,"ok\n");
40   return 1;
41 }
42
43 int 
44 gras_unlock() {
45
46   //  fprintf(stderr,"Release Lock... ");
47 #ifdef HAVE_LIBPTHREAD
48   int ret;
49
50   ret = pthread_mutex_unlock((pthread_mutex_t *)&gras_thelock);
51   if (ret != 0) {
52     fprintf(stderr, "grasReleaseLock: Unable to release lock (errno = %d)!\n", ret);
53     return 0;
54   }
55 #endif
56   //fprintf(stderr,"ok\n");
57   return 1;
58 }
59
60 /* **************************************************************************
61  * Manipulating Callback list
62  * **************************************************************************/
63 gras_cblist_t *gras_cb_get(gras_msgid_t id) {
64   grasProcessData_t *pd=grasProcessDataGet();
65   int i;
66
67   for (i=0 ; i<pd->grasCblListLen && pd->grasCblList[i].id != id ; i++);
68   return i==pd->grasCblListLen ? NULL : &(pd->grasCblList[i]);
69 }
70
71 gras_error_t gras_cb_create(gras_msgid_t message) {
72   grasProcessData_t *pd=grasProcessDataGet();
73
74   if (pd->grasCblListLen++) {
75     pd->grasCblList = (gras_cblist_t *)realloc(pd->grasCblList,
76                                              sizeof(gras_cblist_t)*pd->grasCblListLen);
77   } else {
78     pd->grasCblList = (gras_cblist_t *)malloc(sizeof(gras_cblist_t)*
79                                                       pd->grasCblListLen);
80   }
81   if (!pd->grasCblList) {
82     fprintf(stderr,"PANIC: Malloc error (All callbacks for all hosts are lost)\n");
83     pd->grasCblListLen=0;
84     return malloc_error;
85   }
86
87   pd->grasCblList[pd->grasCblListLen-1].id=message;
88
89   pd->grasCblList[pd->grasCblListLen-1].cbCount=0;
90   pd->grasCblList[pd->grasCblListLen-1].cb=NULL;
91   pd->grasCblList[pd->grasCblListLen-1].cbTTL=NULL;
92
93   return no_error;
94 }
95
96 /* **************************************************************************
97  * Manipulating User Data
98  * **************************************************************************/
99 void *gras_userdata_get(void) { 
100   grasProcessData_t *pd=grasProcessDataGet();
101
102   return pd ? pd->userdata : NULL;
103 }
104
105 void *gras_userdata_set(void *ud) {
106   grasProcessData_t *pd=grasProcessDataGet();
107
108   ASSERT(pd,"ProcessData==NULL  =>  This process did not run grasInit()\n");
109   return pd->userdata=ud;
110 }
111