~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/comm/lists/test_cl_thread_list.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*___INFO__MARK_BEGIN__*/
 
3
/*************************************************************************
 
4
 * 
 
5
 *  The Contents of this file are made available subject to the terms of
 
6
 *  the Sun Industry Standards Source License Version 1.2
 
7
 * 
 
8
 *  Sun Microsystems Inc., March, 2001
 
9
 * 
 
10
 * 
 
11
 *  Sun Industry Standards Source License Version 1.2
 
12
 *  =================================================
 
13
 *  The contents of this file are subject to the Sun Industry Standards
 
14
 *  Source License Version 1.2 (the "License"); You may not use this file
 
15
 *  except in compliance with the License. You may obtain a copy of the
 
16
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
17
 * 
 
18
 *  Software provided under this License is provided on an "AS IS" basis,
 
19
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
20
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
21
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
22
 *  See the License for the specific provisions governing your rights and
 
23
 *  obligations concerning the Software.
 
24
 * 
 
25
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
26
 * 
 
27
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
28
 * 
 
29
 *   All Rights Reserved.
 
30
 * 
 
31
 ************************************************************************/
 
32
/*___INFO__MARK_END__*/
 
33
 
 
34
#include <stdio.h>
 
35
#include <string.h>
 
36
#include <sys/time.h>
 
37
#include <stdlib.h>
 
38
 
 
39
 
 
40
#include "cl_lists.h"
 
41
 
 
42
cl_raw_list_t* thread_list = NULL;
 
43
 
 
44
void *my_thread(void *t_conf);
 
45
void *my_thread_test(void *t_conf);
 
46
 
 
47
 
 
48
 
 
49
#ifdef __CL_FUNCTION__
 
50
#undef __CL_FUNCTION__
 
51
#endif
 
52
#define __CL_FUNCTION__ "main()"
 
53
extern int main(void)
 
54
{
 
55
  cl_thread_settings_t* thread_p = NULL;
 
56
  cl_thread_settings_t* dummy_thread_p = NULL;
 
57
 
 
58
  int count = 0;
 
59
 
 
60
  /* setup thread list */
 
61
  cl_thread_list_setup(&thread_list,"thread list");
 
62
 
 
63
  /* setup first thread */
 
64
  cl_thread_list_create_thread(thread_list, &dummy_thread_p,NULL, "1st thread", 1, my_thread, NULL, NULL);
 
65
 
 
66
  /* setup second thread */
 
67
  cl_thread_list_create_thread(thread_list, &dummy_thread_p, NULL, "2nd thread", 2, my_thread, NULL, NULL);
 
68
 
 
69
  thread_p = cl_thread_list_get_thread_by_id(thread_list, 1);
 
70
  printf("first thread (%s) has id: %d\n", thread_p->thread_name, thread_p->thread_id);
 
71
 
 
72
  thread_p = cl_thread_list_get_thread_by_name(thread_list, "2nd thread");
 
73
  printf("second thread (%s) has id: %d\n" , thread_p->thread_name, thread_p->thread_id); 
 
74
 
 
75
  while (count++ < 10) {
 
76
     int id;
 
77
     
 
78
     printf("we have %ld threads.\n", cl_raw_list_get_elem_count(thread_list));
 
79
 
 
80
     thread_p = cl_thread_list_get_first_thread(thread_list);
 
81
     id = thread_p->thread_id;
 
82
 
 
83
     printf("-----------> delete thread %d ...\n", id);
 
84
     cl_thread_list_delete_thread_by_id(thread_list, id);
 
85
     printf("<----------- delete thread %d done\n", id);
 
86
 
 
87
     printf("-----------> add thread ...\n");
 
88
     cl_thread_list_create_thread(thread_list, &dummy_thread_p, NULL,"new thread", id, my_thread, NULL, NULL);
 
89
     printf("<----------- add thread done\n");
 
90
 
 
91
  }
 
92
 
 
93
  /* delete all threads */
 
94
  while ( (thread_p=cl_thread_list_get_first_thread(thread_list)) != NULL ) {
 
95
     int id = thread_p->thread_id;
 
96
     printf("-----------> delete thread %d ...\n",id );
 
97
 
 
98
     cl_thread_list_delete_thread(thread_list, thread_p);
 
99
     printf("<----------- delete thread %d done\n",id );
 
100
 
 
101
  }
 
102
 
 
103
  cl_thread_list_cleanup(&thread_list);
 
104
  printf("main done\n");
 
105
  return 0;
 
106
}
 
107
 
 
108
 
 
109
 
 
110
#ifdef __CL_FUNCTION__
 
111
#undef __CL_FUNCTION__
 
112
#endif
 
113
#define __CL_FUNCTION__ "my_thread()"
 
114
void *my_thread(void *t_conf) {
 
115
   cl_thread_settings_t* thread_p = NULL;
 
116
   int counter = 0;
 
117
   int do_exit = 0;
 
118
   /* get pointer to cl_thread_settings_t struct */
 
119
   cl_thread_settings_t *thread_config = (cl_thread_settings_t*)t_conf; 
 
120
 
 
121
   /* setup thread */
 
122
   printf("thread %d: initialize\n", thread_config->thread_id);
 
123
 
 
124
 
 
125
 
 
126
   /* thread init done, trigger startup conditon variable*/
 
127
   cl_thread_func_startup(thread_config);
 
128
 
 
129
 
 
130
   printf("thread %d: enter mainloop\n", thread_config->thread_id);
 
131
 
 
132
   /* ok, thread main */
 
133
   while (do_exit == 0) {
 
134
      int ret_val;
 
135
 
 
136
      printf("thread %d: try locking thread_list ...\n", thread_config->thread_id);
 
137
 
 
138
      /* check for cancel */
 
139
      cl_thread_func_testcancel(thread_config);
 
140
      if (cl_raw_list_lock(thread_list) == CL_RETVAL_OK) {
 
141
         int id;
 
142
         printf("thread %d: locked thread_list\n", thread_config->thread_id);
 
143
 
 
144
         if (thread_config->thread_id == 1) {
 
145
            thread_p = cl_thread_list_get_thread_by_id(thread_list,2);
 
146
            id = 2;
 
147
         } else {
 
148
            thread_p = cl_thread_list_get_thread_by_id(thread_list,1);
 
149
            id = 1;
 
150
         }
 
151
         if (thread_p) {
 
152
            if (thread_config->thread_id == 1) {
 
153
               cl_thread_trigger_event(thread_p);
 
154
               counter++;
 
155
               printf("thread %d: triggered %d events\n", thread_config->thread_id, counter); 
 
156
            }
 
157
         } else {
 
158
            printf("thread %d: thread %d not found\n", thread_config->thread_id,id);
 
159
         }
 
160
         printf("thread %d: unlocking thread_list\n", thread_config->thread_id);
 
161
 
 
162
         cl_raw_list_unlock(thread_list);
 
163
      }
 
164
 
 
165
      if ((ret_val = cl_thread_wait_for_event(thread_config,0,10000)) != CL_RETVAL_OK) {  /* nothing to do */
 
166
         switch(ret_val) {
 
167
            case CL_RETVAL_CONDITION_WAIT_TIMEOUT:
 
168
/*               printf("thread %d: got timeout\n", thread_config->thread_id);  */
 
169
               break;
 
170
            default: {
 
171
               printf("thread %d: got error: %d\n", thread_config->thread_id, ret_val);
 
172
               do_exit = 1;
 
173
            }
 
174
         }
 
175
      }
 
176
   }
 
177
 
 
178
   printf("thread %d: exit\n", thread_config->thread_id);
 
179
   
 
180
   /* at least set exit state */
 
181
   cl_thread_func_cleanup(thread_config);  
 
182
   return(NULL);
 
183
}
 
184
 
 
185