Implemented all system wake up timer functions.

This commit is contained in:
Yuval Dolev 2019-12-30 00:34:53 +02:00
parent 76069e9ac1
commit dc213307a9
2 changed files with 59 additions and 42 deletions

View File

@ -83,13 +83,13 @@
//////////////////////////////// ////////////////////////////////
typedef i32 Win32_Object_Kind; typedef i32 Mac_Object_Kind;
enum{ enum{
Win32ObjectKind_ERROR = 0, MacObjectKind_ERROR = 0,
Win32ObjectKind_Timer = 1, MacObjectKind_Timer = 1,
Win32ObjectKind_Thread = 2, MacObjectKind_Thread = 2,
Win32ObjectKind_Mutex = 3, MacObjectKind_Mutex = 3,
Win32ObjectKind_CV = 4, MacObjectKind_CV = 4,
}; };
struct Mac_Object{ struct Mac_Object{
@ -143,19 +143,19 @@ mac_alloc_object(Mac_Object_Kind kind){
if (!result){ if (!result){
i32 count = 512; i32 count = 512;
Mac_Object* objects = (Mac_Object*)system_memory_allocate(count * sizeof(Mac_Object), file_name_line_number); Mac_Object *objects = (Mac_Object*)system_memory_allocate(count * sizeof(Mac_Object), file_name_line_number_lit_u8);
// NOTE(yuval): Link the first chain of the dll to the sentinel // NOTE(yuval): Link the first node of the dll to the sentinel
objects[0].node.prev = &mac_vars.free_mac_objects; objects[0].node.prev = &mac_vars.free_mac_objects;
mac_vars.free_mac_objects.next = &objects[0].node; mac_vars.free_mac_objects.next = &objects[0].node;
// NOTE(yuval): Link all dll chains to each other // NOTE(yuval): Link all dll nodes to each other
for (i32 chain_index = 1; chain_index < count; chain_index += 1){ for (i32 chain_index = 1; chain_index < count; chain_index += 1){
objects[chain_index - 1].node.next = &objects[chain_index].node; objects[chain_index - 1].node.next = &objects[chain_index].node;
objects[chain_index].node.prev = &objects[chain_index - 1].node; objects[chain_index].node.prev = &objects[chain_index - 1].node;
} }
// NOTE(yuval): Link the last chain of the dll to the sentinel // NOTE(yuval): Link the last node of the dll to the sentinel
objects[count - 1].node.next = &mac_vars.free_mac_objects; objects[count - 1].node.next = &mac_vars.free_mac_objects;
mac_vars.free_mac_objects.prev = &objects[count - 1].node; mac_vars.free_mac_objects.prev = &objects[count - 1].node;
@ -170,6 +170,14 @@ mac_alloc_object(Mac_Object_Kind kind){
return(result); return(result);
} }
function
mac_free_object(Mac_Object *object){
if (object->node.next != 0){
dll_remove(&object->node);
}
dll_insert(&mac_vars.free_mac_objects, &object->node);
}
//////////////////////////////// ////////////////////////////////

View File

@ -273,9 +273,6 @@ system_save_file_sig(){
i32 fd = open(file_name, O_WRONLY | O_TRUNC | O_CREAT, 00640); i32 fd = open(file_name, O_WRONLY | O_TRUNC | O_CREAT, 00640);
if (fd != -1) { if (fd != -1) {
u8* data_str = data.str;
u64 data_size = data.size;
do{ do{
ssize_t bytes_written = write(fd, data.str, data.size); ssize_t bytes_written = write(fd, data.str, data.size);
if (bytes_written == -1){ if (bytes_written == -1){
@ -364,9 +361,9 @@ system_now_time_sig(){
u64 now = mach_absolute_time(); u64 now = mach_absolute_time();
// NOTE(yuval): Now time nanoseconds conversion // NOTE(yuval): Now time nanoseconds conversion
f64 now_nano = (f64)(((f64)now) * f64 now_nano = (f64)((f64)now *
((f64)mac_vars.timebase_info.numer) / (f64)mac_vars.timebase_info.numer /
((f64)mac_vars.timebase_info.denom)); (f64)mac_vars.timebase_info.denom);
// NOTE(yuval): Conversion to useconds // NOTE(yuval): Conversion to useconds
u64 result = (u64)(now_nano * 1.0E-3); u64 result = (u64)(now_nano * 1.0E-3);
@ -375,24 +372,36 @@ system_now_time_sig(){
function function
system_wake_up_timer_create_sig(){ system_wake_up_timer_create_sig(){
Plat_Handle result = {}; Mac_Object *object = mac_alloc_object(MacObjectKind_Timer);
dll_insert(&mac_vars.timer_objects, &object->node);
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.0 object->timer.timer = nil;
target: view
selector: @selector(requestDisplay)
userInfo: nil repeats:NO];
Plat_Handle result = mac_to_plat_handle(object);
return(result); return(result);
} }
function function
system_wake_up_timer_release_sig(){ system_wake_up_timer_release_sig(){
NotImplemented; Mac_Object *object = (Mac_Object*)mac_to_object(handle);
if (object->kind == MacObjectKind_Timer){
if ((object->timer != nil) && [object->timer isValid]) {
[object->timer invalidate];
mac_free_object(object);
}
}
} }
function function
system_wake_up_timer_set_sig(){ system_wake_up_timer_set_sig(){
NotImplemented; Mac_Object *object = (Mac_Object*)mac_to_object(handle);
if (object->kind == MacObjectKind_Timer){
f64 time_seconds = ((f64)time_milliseconds / 1000.0);
object->timer = [NSTimer scheduledTimerWithTimeInterval: time_seconds
target: mac_vars.view
selector: @selector(requestDisplay)
userInfo: nil repeats:NO];
}
} }
function function