-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invoked function and callback function can now accept a context point…
…er (breaking change)
- Loading branch information
1 parent
ea1d272
commit fa2626e
Showing
11 changed files
with
155 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
#ifndef __FOREVER_H__ | ||
#define __FOREVER_H__ | ||
|
||
unsigned int forever(void (*fn)()); | ||
unsigned int forever_with_options(void (*fn)(), const unsigned int /* max retries */, const unsigned int /* interval between invocations in milliseconds */); | ||
unsigned int forever_with_callback(void (*fn)(), int (*callback)(const unsigned char, int)); | ||
unsigned int forever(void (*fn)(void *), void *); | ||
unsigned int forever_with_options(void (*fn)(void *), void *, const unsigned int /* max retries */, const unsigned int /* interval between invocations in milliseconds */); | ||
unsigned int forever_with_callback(void (*fn)(void *), void *, int (*callback)(void *, const unsigned char, int)); | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "forever.h" | ||
#include "test.h" | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
struct Context | ||
{ | ||
int counter; | ||
}; | ||
|
||
|
||
int callback(void *context, const unsigned char started, int stat_loc) | ||
{ | ||
((struct Context *)context)->counter++; | ||
assert_true(started); | ||
assert_num_equal(stat_loc, 0); | ||
|
||
if (((struct Context *)context)->counter < 4) | ||
{ | ||
return(500); | ||
} | ||
|
||
return(-1); | ||
} | ||
|
||
|
||
void fn(void *context) | ||
{ | ||
assert_true(context != NULL); | ||
exit(0); | ||
} | ||
|
||
|
||
void test_impl() | ||
{ | ||
struct Context *context = malloc(sizeof(struct Context)); | ||
|
||
context->counter = 0; | ||
|
||
clock_t time = clock(); | ||
unsigned int counter = forever_with_callback(fn, context, callback); | ||
free(context); | ||
|
||
assert_num_equal(counter, 4); | ||
assert_true(clock() - time >= 2); | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
test_run(test_impl); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "forever.h" | ||
#include "test.h" | ||
#include <stdlib.h> | ||
|
||
struct Context | ||
{ | ||
int counter; | ||
}; | ||
|
||
|
||
void fn(void *context) | ||
{ | ||
((struct Context *)context)->counter++; | ||
exit(0); | ||
} | ||
|
||
|
||
void test_impl() | ||
{ | ||
struct Context *context = malloc(sizeof(struct Context)); | ||
|
||
context->counter = 0; | ||
|
||
unsigned int counter = forever_with_options(fn, context, 10, 1); | ||
|
||
assert_num_equal(counter, 10); | ||
assert_num_equal(context->counter, 0); | ||
|
||
free(context); | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
test_run(test_impl); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters