1
/*
2
 *  Copyright (c) 2009 Nokia 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
7
 *  met:
8
 * 
9
 *  * Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 *  * Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *  * Neither the name of the Nokia Corporation nor the names of its
15
 *    contributors may be used to endorse or promote products derived from
16
 *    this software without specific prior written permission.
17
 * 
18
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19
 *  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20
 *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21
 *  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22
 *  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * Author(s): Mikko Hurskainen, Ville Kankainen
31
 */
32
33
#ifndef __PLATFORM_H__
34
#define __PLATFORM_H__
35
36
#include "utils.h" /* some useful macros, such as h_assert and byteswapping */
37
38
#ifdef HAVE_STRING_H
39
#include <string.h> /* for memset and memcpy */
40
#endif
41
42
#ifdef HAVE_STDLIB_H
43
#include <stdlib.h> /* for size_t */
44
#else
45
#ifndef _SIZE_T_DECLARED
46
typedef unsigned int size_t;
47
#endif
48
#endif
49
50
#include <time.h> /* for time_t */
51
52
/* Declarations */
53
struct nota_lock;
54
typedef struct nota_lock nota_lock_t;
55
56
struct nota_thread;
57
typedef struct nota_thread nota_thread_t;
58
59
struct nota_semaphore;
60
typedef struct nota_semaphore nota_semaphore_t;
61
62
63
/*
64
 * for now these are macros... are there any platforms that
65
 * do not support these?
66
 */
67
68
69
#ifndef nota_memset
70
#define nota_memset(x,y,z)    memset((x),(y),(z))
71
#endif
72
73
#ifndef nota_memcpy
74
#define nota_memcpy(x,y,z)    memcpy((x),(y),(z))
75
#endif
76
77
78
/* Function pointer declaration */
79
typedef void* (*nota_thread_func)(void* arg);
80
81
#ifdef __cplusplus
82
extern "C" {
83
#endif
84
  /* Functions */
85
86
  /* Allocates a new lock */
87
  int nota_lock_alloc(nota_lock_t** lock);
88
  /* Frees a lock */
89
  int nota_lock_free(nota_lock_t** lock);
90
  /* Acquires the lock (exclusivity). Blocks if the lock is not free */
91
  int nota_lock_get(nota_lock_t* lock);
92
  /* Releases the lock */
93
  int nota_lock_release(nota_lock_t* lock);
94
  /* Creates a thread */
95
  int nota_thread_create(nota_thread_t** thread, nota_thread_func func, void* arg);
96
  /* Joins a thread. Blocks until other thread exits */
97
  int nota_thread_join(nota_thread_t** thread);
98
  /* Allocates a semaphore with initial count value */
99
  int nota_semaphore_alloc(nota_semaphore_t** semaphore, int initial_value);
100
  /* Frees a semaphore */
101
  int nota_semaphore_free(nota_semaphore_t** semaphore);
102
  /* Increments semaphore */
103
  int nota_semaphore_up(nota_semaphore_t* semaphore);
104
  /* Decrements the semaphore. If the count is zero, will block */
105
  int nota_semaphore_down(nota_semaphore_t* semaphore);
106
  /* Thread sleep for milliseconds */
107
  int nota_sleep_msec(unsigned int milliseconds);
108
  /* Thread sleep for milliseconds */
109
  int nota_sleep_usec(unsigned int microseconds);
110
  /* Gets current time in seconds */
111
  time_t nota_get_time_in_sec(void);
112
  /* Allocates memory */
113
  void* nota_malloc(size_t size);
114
  /* Reallocates memory */
115
  void* nota_mrealloc(void* ptr, size_t new_size);
116
  /* frees memory */
117
  void nota_free(void* ptr);
118
119
  /* sets errno for H_IF api */
120
  void h_if_errno_set(int error_status);
121
122
123
124
125
/* BELOW: remove these from l_in, use the ones above instead */
126
127
#include <pthread.h>
128
129
typedef pthread_mutex_t llock_t;
130
typedef pthread_cond_t lcond_t;
131
132
#define llock(lock)            pthread_mutex_lock(lock)
133
#define lunlock(lock)          pthread_mutex_unlock(lock)
134
#define lcond_broadcast(cond)  pthread_cond_broadcast(cond)
135
#define lcond_wait(cond, lock) pthread_cond_wait(cond, lock)
136
#define llock_init(lock)       pthread_mutex_init(lock, NULL)
137
#define llock_destroy(lock)    pthread_mutex_destroy(lock)
138
#define lcond_init(cond)       pthread_cond_init(cond, NULL)
139
#define lcond_destroy(cond)    pthread_cond_destroy(cond)
140
141
/*
142
 * Unhandled:
143
 *   clock_gettime(&ts)
144
 *   getenv
145
 */
146
147
#ifdef __cplusplus
148
};
149
#endif
150
151
#endif /*__PLATFORM_H__*/