Skip to content

Commit 6e09ed0

Browse files
committed
Refactor time functions to own source
1 parent 94fb257 commit 6e09ed0

File tree

5 files changed

+100
-41
lines changed

5 files changed

+100
-41
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ Function|Description
3030

3131
I ported the examples found in the protothreads distribution to async.h. Here
3232
is the async.h equivalent of the protothreads sample on the home page:
33-
```C
33+
```c
3434
#include "async.h"
35+
#include "async-time.h"
3536

3637
struct async pt;
3738
struct timer timer;
@@ -41,7 +42,7 @@ async example(struct async *pt) {
4142

4243
while(1) {
4344
if(initiate_io()) {
44-
timer_start(&timer);
45+
timer_set(&timer);
4546
await(io_completed() || timer_expired(&timer));
4647
read_data();
4748
}
@@ -55,12 +56,13 @@ to accept the async structure/local continuation as an argument.
5556
5657
Here is the same example as above, but where the timer is lifted to
5758
a local parameter:
58-
```C
59+
```c
5960
#include "async.h"
61+
#include "async-time.h"
6062
6163
typedef struct {
6264
async_state; // declare the asynchronous state
63-
timer timer; // declare local state
65+
struct timer timer; // declare local state
6466
} example_state;
6567
example_state pt;
6668
@@ -69,7 +71,7 @@ async example(example_state *pt) {
6971
7072
while(1) {
7173
if(initiate_io()) {
72-
timer_start(&pt->timer);
74+
timer_set(&pt->timer);
7375
await(io_completed() || timer_expired(&pt->timer));
7476
read_data();
7577
}

async/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CC = gcc
22
CCFlags = -Wall
33
BUILD_DIR = build
44

5-
SRC = example-buffer.c example-codelock.c example-small.c main.c
5+
SRC = async-time.c example-buffer.c example-codelock.c example-small.c main.c
66
OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(SRC))
77

88
all : $(OBJ)

async/async-time.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifdef _WIN32
2+
#include <windows.h>
3+
#else
4+
#include <unistd.h>
5+
#include <sys/time.h>
6+
#endif
7+
8+
#include "async.h"
9+
#include "async-time.h"
10+
11+
#ifdef _WIN32
12+
13+
static int clock_time(void)
14+
{
15+
return (int)GetTickCount();
16+
}
17+
18+
#else /* _WIN32 */
19+
20+
static int clock_time(void)
21+
{
22+
struct timeval tv;
23+
struct timezone tz;
24+
gettimeofday(&tv, &tz);
25+
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
26+
}
27+
28+
#endif /* _WIN32 */
29+
30+
int timer_expired(struct timer *t)
31+
{
32+
return (int)(clock_time() - t->start) >= (int)t->interval;
33+
}
34+
35+
void timer_set(struct timer *t, unsigned int interval)
36+
{
37+
t->interval = interval;
38+
t->start = clock_time();
39+
}
40+
41+
async async_sleep(struct async_sleep_state *state, unsigned int usecs)
42+
{
43+
async_begin(state);
44+
45+
timer_set(&state->timer, usecs);
46+
await(timer_expired(&state->timer));
47+
48+
async_end;
49+
}

async/async-time.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "async.h"
2+
3+
/**
4+
* Timer structure for tracking when a timer has expired
5+
*/
6+
struct timer
7+
{
8+
unsigned int start;
9+
unsigned int interval;
10+
};
11+
12+
/**
13+
* State structure for calls to the async_sleep function
14+
*/
15+
struct async_sleep_state
16+
{
17+
async_state;
18+
struct timer timer;
19+
};
20+
21+
/**
22+
* Initialize a timer with a specified time duration
23+
*
24+
* @param t timer struct to initialize
25+
* @param usecs number of milliseconds the timer is good for
26+
*/
27+
void timer_set(struct timer *t, unsigned int usecs);
28+
29+
/**
30+
* Determine if the supplied timer is expired
31+
*
32+
* @param t timer struct to examine for expiration
33+
*/
34+
int timer_expired(struct timer *t);
35+
36+
/**
37+
* Sleep asynchronously for a specified duration
38+
*
39+
* @param state async_sleep_state structure to hold state of operation
40+
* @param usecs number of milliseconds to sleep for
41+
*/
42+
async async_sleep(struct async_sleep_state *state, unsigned int usecs);

async/example-codelock.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,8 @@
6161
#include <stdio.h>
6262

6363
#include "async.h"
64+
#include "async-time.h"
6465

65-
/*---------------------------------------------------------------------------*/
66-
/*
67-
* The following definitions are just for the simple timer library
68-
* used in this example. The actual implementation of the functions
69-
* can be found at the end of this file.
70-
*/
71-
struct timer { int start, interval; };
72-
static int timer_expired(struct timer *t);
73-
static void timer_set(struct timer *t, int usecs);
7466
/*---------------------------------------------------------------------------*/
7567
/*
7668
* This example uses two timers: one for the code lock async and
@@ -374,30 +366,4 @@ example_codelock(void)
374366

375367
return 0;
376368
}
377-
/*---------------------------------------------------------------------------*/
378-
/*
379-
* Finally, the implementation of the simple timer library follows.
380-
*/
381-
#ifdef _WIN32
382-
383-
static int clock_time(void)
384-
{ return (int)GetTickCount(); }
385369

386-
#else /* _WIN32 */
387-
388-
static int clock_time(void)
389-
{
390-
struct timeval tv;
391-
struct timezone tz;
392-
gettimeofday(&tv, &tz);
393-
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
394-
}
395-
396-
#endif /* _WIN32 */
397-
398-
static int timer_expired(struct timer *t)
399-
{ return (int)(clock_time() - t->start) >= (int)t->interval; }
400-
401-
static void timer_set(struct timer *t, int interval)
402-
{ t->interval = interval; t->start = clock_time(); }
403-
/*---------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)