OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [libmcapi/] [mcapi/] [linux/] [mcapi_os.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*
2
 * Copyright (c) 2010, Mentor Graphics Corporation
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
#include <openmcapi.h>
31
 
32
MCAPI_MUTEX MCAPI_Mutex;
33
pthread_t   MCAPI_Control_Task_TCB;
34
 
35
static inline long ms_to_s(long ms)
36
{
37
    return ms / 1000;
38
}
39
 
40
static inline long ns_to_s(long ns)
41
{
42
    return ns / 1000000000;
43
}
44
 
45
static inline long ms_to_ns(long ms)
46
{
47
    return ms * 1000000;
48
}
49
 
50
/*************************************************************************
51
*
52
*   FUNCTION
53
*
54
*       MCAPI_Init_OS
55
*
56
*   DESCRIPTION
57
*
58
*       Initializes OS specific data structures.
59
*
60
*   INPUTS
61
*
62
*       None.
63
*
64
*   OUTPUTS
65
*
66
*       MCAPI_SUCCESS
67
*       MCAPI_ERR_GENERAL
68
*
69
*************************************************************************/
70
mcapi_status_t MCAPI_Init_OS(void)
71
{
72
    int status;
73
 
74
    /* Create the task that will be used for receiving status
75
     * messages.
76
     */
77
    status = pthread_create(&MCAPI_Control_Task_TCB, NULL,
78
                            mcapi_process_ctrl_msg, NULL);
79
 
80
    if (status)
81
        return MCAPI_ERR_GENERAL;
82
 
83
    return MCAPI_SUCCESS;
84
}
85
 
86
/*************************************************************************
87
*
88
*   FUNCTION
89
*
90
*       MCAPI_Exit_OS
91
*
92
*   DESCRIPTION
93
*
94
*       Release OS specific data structures.
95
*
96
*   INPUTS
97
*
98
*       None.
99
*
100
*   OUTPUTS
101
*
102
*       None.
103
*
104
*************************************************************************/
105
void MCAPI_Exit_OS(void)
106
{
107
}
108
 
109
/*************************************************************************
110
*
111
*   FUNCTION
112
*
113
*       MCAPI_Resume_Task
114
*
115
*   DESCRIPTION
116
*
117
*       Resumes a suspended system task.
118
*
119
*   INPUTS
120
*
121
*       *request                The request structure on which the
122
*                               suspended task is suspended.
123
*
124
*   OUTPUTS
125
*
126
*       MCAPI_SUCCESS
127
*       MCAPI_ERR_GENERAL
128
*
129
*************************************************************************/
130
mcapi_status_t MCAPI_Resume_Task(mcapi_request_t *request)
131
{
132
    int status;
133
 
134
    /* If multiple requests are suspending on the same condition, we use
135
     * mcapi_cond_ptr. */
136
    if (request->mcapi_cond.mcapi_cond_ptr)
137
        status = pthread_cond_signal(request->mcapi_cond.mcapi_cond_ptr);
138
    else
139
        status = pthread_cond_signal(&request->mcapi_cond.mcapi_cond);
140
 
141
    if (status)
142
        return MCAPI_ERR_GENERAL;
143
 
144
    return MCAPI_SUCCESS;
145
}
146
 
147
/*************************************************************************
148
*
149
*   FUNCTION
150
*
151
*       MCAPI_Suspend_Task
152
*
153
*   DESCRIPTION
154
*
155
*       Suspends a system task.
156
*
157
*   INPUTS
158
*
159
*       *node_data              A pointer to the global node data
160
*                               structure.
161
*       *request                A pointer to the request associated
162
*                               with the thread being suspended.
163
*       *mcapi_os               A pointer to the OS specific structure
164
*                               containing suspend/resume data.
165
*       timeout                 The number of milliseconds to suspend
166
*                               pending completion of the request.
167
*
168
*   OUTPUTS
169
*
170
*       MCAPI_SUCCESS
171
*       MCAPI_ERR_GENERAL
172
*
173
*************************************************************************/
174
mcapi_status_t MCAPI_Suspend_Task(MCAPI_GLOBAL_DATA *node_data,
175
                                  mcapi_request_t *request,
176
                                  MCAPI_COND_STRUCT *condition,
177
                                  mcapi_timeout_t timeout)
178
{
179
    struct timespec ts;
180
    int status = 0;
181
 
182
    /* If a request structure was passed into the routine. */
183
    if (request)
184
    {
185
        /* Initialize the condition variable with default parameters. */
186
        status = pthread_cond_init(&request->mcapi_cond.mcapi_cond, NULL);
187
 
188
        if (status == 0)
189
        {
190
            /* Add the request to the queue of pending requests. */
191
            mcapi_enqueue(&node_data->mcapi_local_req_queue, request);
192
        }
193
    }
194
 
195
    if (status == 0)
196
    {
197
        /* If no timeout value was specified. */
198
        if (timeout == MCAPI_TIMEOUT_INFINITE)
199
        {
200
            status = pthread_cond_wait(&condition->mcapi_cond, &MCAPI_Mutex);
201
        }
202
        else
203
        {
204
            long subsecond_ms;
205
            long total_ns;
206
 
207
            clock_gettime(CLOCK_REALTIME, &ts);
208
 
209
            /* Add timeout to ts, accounting for nsec overflow. */
210
            subsecond_ms = timeout % 1000;
211
            total_ns = ts.tv_nsec + ms_to_ns(subsecond_ms);
212
            ts.tv_sec += ms_to_s(timeout) + ns_to_s(total_ns);
213
            ts.tv_nsec = total_ns % 1000000000;
214
 
215
            status = pthread_cond_timedwait(&condition->mcapi_cond,
216
                                            &MCAPI_Mutex, &ts);
217
        }
218
 
219
        /* Uninitialize the condition variable. */
220
        status = pthread_cond_destroy(&condition->mcapi_cond);
221
    }
222
 
223
    if (status)
224
        return MCAPI_ERR_GENERAL;
225
 
226
    return MCAPI_SUCCESS;
227
}
228
 
229
/*************************************************************************
230
*
231
*   FUNCTION
232
*
233
*       MCAPI_Cleanup_Task
234
*
235
*   DESCRIPTION
236
*
237
*       Terminates the current thread.
238
*
239
*   INPUTS
240
*
241
*       None.
242
*
243
*   OUTPUTS
244
*
245
*       None.
246
*
247
*************************************************************************/
248
void MCAPI_Cleanup_Task(void)
249
{
250
    pthread_exit(NULL);
251
}
252
 
253
/*************************************************************************
254
*
255
*   FUNCTION
256
*
257
*       MCAPI_Init_Condition
258
*
259
*   DESCRIPTION
260
*
261
*       Sets an OS specific condition for resuming a task in the future.
262
*
263
*   INPUTS
264
*
265
*       *os_cond                A pointer to the OS specific structure
266
*                               containing suspend/resume data.
267
*
268
*   OUTPUTS
269
*
270
*       None.
271
*
272
*************************************************************************/
273
void MCAPI_Init_Condition(MCAPI_COND_STRUCT *condition)
274
{
275
    /* Initialize the condition variable with default parameters. */
276
    pthread_cond_init(&condition->mcapi_cond, NULL);
277
}
278
 
279
/*************************************************************************
280
*
281
*   FUNCTION
282
*
283
*       MCAPI_Set_Condition
284
*
285
*   DESCRIPTION
286
*
287
*       Sets an OS specific condition for resuming a task in the future.
288
*
289
*   INPUTS
290
*
291
*       *request                A request structure that will trigger
292
*                               a future task resume.
293
*       *os_cond                The condition to use for resuming the
294
*                               task.
295
*
296
*   OUTPUTS
297
*
298
*       None.
299
*
300
*************************************************************************/
301
void MCAPI_Set_Condition(mcapi_request_t *request, MCAPI_COND_STRUCT *condition)
302
{
303
    request->mcapi_cond.mcapi_cond_ptr = &condition->mcapi_cond;
304
}
305
 
306
/*************************************************************************
307
*
308
*   FUNCTION
309
*
310
*       MCAPI_Clear_Condition
311
*
312
*   DESCRIPTION
313
*
314
*       Clears a previously set OS condition.
315
*
316
*   INPUTS
317
*
318
*       *request                A request structure that will trigger
319
*                               a future task resume.
320
*
321
*   OUTPUTS
322
*
323
*       None.
324
*
325
*************************************************************************/
326
void MCAPI_Clear_Condition(mcapi_request_t *request)
327
{
328
    request->mcapi_cond.mcapi_cond_ptr = MCAPI_NULL;
329
}
330
 
331
/*************************************************************************
332
*
333
*   FUNCTION
334
*
335
*       MCAPI_Create_Mutex
336
*
337
*   DESCRIPTION
338
*
339
*       Creates a system mutex.
340
*
341
*   INPUTS
342
*
343
*       *mutex                  Pointer to the mutex control block.
344
*       *name                   Name of the mutex.
345
*
346
*   OUTPUTS
347
*
348
*       MCAPI_SUCCESS
349
*
350
*************************************************************************/
351
mcapi_status_t MCAPI_Create_Mutex(MCAPI_MUTEX *mutex, char *name)
352
{
353
    int status;
354
 
355
    status = pthread_mutex_init(mutex, NULL);
356
 
357
    if (status)
358
        return MCAPI_ERR_GENERAL;
359
 
360
    return MCAPI_SUCCESS;
361
}
362
 
363
/*************************************************************************
364
*
365
*   FUNCTION
366
*
367
*       MCAPI_Delete_Mutex
368
*
369
*   DESCRIPTION
370
*
371
*       Destroyes a system mutex.
372
*
373
*   INPUTS
374
*
375
*       *mutex                  Pointer to the mutex control block.
376
*
377
*   OUTPUTS
378
*
379
*       MCAPI_SUCCESS
380
*       MCAPI_ERR_GENERAL
381
*
382
*************************************************************************/
383
mcapi_status_t MCAPI_Delete_Mutex(MCAPI_MUTEX *mutex)
384
{
385
    int status;
386
 
387
    status = pthread_mutex_destroy(mutex);
388
 
389
    if (status)
390
        return MCAPI_ERR_GENERAL;
391
 
392
    return MCAPI_SUCCESS;
393
}
394
 
395
/*************************************************************************
396
*
397
*   FUNCTION
398
*
399
*       MCAPI_Obtain_Mutex
400
*
401
*   DESCRIPTION
402
*
403
*       Obtains a system mutex.
404
*
405
*   INPUTS
406
*
407
*       *mutex              Pointer to the mutex control block.
408
*
409
*   OUTPUTS
410
*
411
*       MCAPI_SUCCESS
412
*       MCAPI_ERR_GENERAL
413
*
414
*************************************************************************/
415
mcapi_status_t MCAPI_Obtain_Mutex(MCAPI_MUTEX *mutex)
416
{
417
    int status;
418
 
419
    status = pthread_mutex_lock(mutex);
420
 
421
    if (status)
422
        return MCAPI_ERR_GENERAL;
423
 
424
    return MCAPI_SUCCESS;
425
}
426
 
427
/*************************************************************************
428
*
429
*   FUNCTION
430
*
431
*       MCAPI_Release_Mutex
432
*
433
*   DESCRIPTION
434
*
435
*       Releases a system mutex.
436
*
437
*   INPUTS
438
*
439
*       *mutex              Pointer to the mutex control block.
440
*
441
*   OUTPUTS
442
*
443
*       MCAPI_SUCCESS
444
*       MCAPI_ERR_GENERAL
445
*
446
*************************************************************************/
447
mcapi_status_t MCAPI_Release_Mutex(MCAPI_MUTEX *mutex)
448
{
449
    int status;
450
 
451
    status = pthread_mutex_unlock(mutex);
452
 
453
    if (status)
454
        return MCAPI_ERR_GENERAL;
455
 
456
    return MCAPI_SUCCESS;
457
}
458
 
459
/*************************************************************************
460
*
461
*   FUNCTION
462
*
463
*       MCAPI_Set_RX_Event
464
*
465
*   DESCRIPTION
466
*
467
*       Sets an event indicating that data is ready to be received.
468
*
469
*   INPUTS
470
*
471
*       None.
472
*
473
*   OUTPUTS
474
*
475
*       MCAPI_SUCCESS
476
*
477
*************************************************************************/
478
mcapi_status_t MCAPI_Set_RX_Event(void)
479
{
480
    mcapi_rx_data();
481
 
482
    return MCAPI_SUCCESS;
483
}
484
 
485
/*************************************************************************
486
*
487
*   FUNCTION
488
*
489
*       MCAPI_Lock_RX_Queue
490
*
491
*   DESCRIPTION
492
*
493
*       Protect RX queue from concurrent accesses.  No work is required
494
*       in this routine since Linux receives data from the context of
495
*       the driver RX thread.
496
*
497
*   INPUTS
498
*
499
*       None.
500
*
501
*   OUTPUTS
502
*
503
*       Unused.
504
*
505
*************************************************************************/
506
mcapi_int_t MCAPI_Lock_RX_Queue(void)
507
{
508
    return 0;
509
}
510
 
511
/*************************************************************************
512
*
513
*   FUNCTION
514
*
515
*       MCAPI_Unlock_RX_Queue
516
*
517
*   DESCRIPTION
518
*
519
*       Protect RX queue from concurrent accesses.  No work is required
520
*       in this routine since Linux receives data from the context of
521
*       the driver RX thread.
522
*
523
*   INPUTS
524
*
525
*       cookie                  Unused.
526
*
527
*   OUTPUTS
528
*
529
*       None.
530
*
531
*************************************************************************/
532
void MCAPI_Unlock_RX_Queue(mcapi_int_t cookie)
533
{
534
}
535
 
536
void MCAPI_Sleep(unsigned int secs)
537
{
538
        /* XXX We can use select() to implement sub-second sleeps in the future. */
539
    sleep(secs);
540
}
541
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.