Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage of clock and CLOCKS_PER_SEC #590

Merged
merged 3 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ee/libcglue/samples/nanosleep/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char *argv[])
struct timespec tv = {0};
tv.tv_sec = 1;
tv.tv_nsec = 0;
error_tolerance = 5; // 5 miliseconds of error tolerance
error_tolerance = 200; // 200 miliseconds of error tolerance
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is room for improvement in the timer, we should be more accurate.


#if defined(SCREEN_DEBUG)
init_scr();
Expand All @@ -51,13 +51,13 @@ int main(int argc, char *argv[])
second_waited += tv.tv_sec;
tv.tv_sec++;
}
diff = (clock() - start);
diff_error = (second_waited * 1000) - diff;
diff = (clock() - start)/ 1000;
diff_error = abs((second_waited * 1000) - diff);

custom_printf("Checking if we have waited %i seconds...\n", second_waited);
custom_printf("We have waited: %lu milliseconds\n", diff);

if (abs(diff_error) < error_tolerance) {
if (diff_error < error_tolerance) {
custom_printf("nanosecond looks to works properly\n");
} else {
custom_printf("nanosecond is not accurate there is a difference of %lu milliseconds \n", diff_error);
Expand Down
2 changes: 1 addition & 1 deletion ee/libcglue/src/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ int _gettimeofday(struct timeval *tv, struct timezone *tz)

#ifdef F__times
clock_t _times(struct tms *buffer) {
clock_t clk = GetTimerSystemTime() / (kBUSCLK / CLOCKS_PER_SEC);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even being the same value, I think that using CLOCKS_PER_SEC could be "confusing"

clock_t clk = GetTimerSystemTime() / (kBUSCLK / (1000 * 1000));

if (buffer != NULL) {
buffer->tms_utime = clk;
Expand Down
2 changes: 1 addition & 1 deletion ee/libpthreadglue/src/osal.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle semHandle, uns
if (pTimeout == NULL) {
timeout = 0;
} else {
timeout = *pTimeout;
timeout = *pTimeout * 1000;
}

while (1) {
Expand Down
17 changes: 5 additions & 12 deletions ee/network/tcpip/src/sys_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ static void free_msg(arch_message *msg)
SignalSema(MsgCountSema);
}

static inline u32_t ComputeTimeDiff(u32 start, u32 end)
{
u32 NumTicksElasped=(end<start)?UINT_MAX-start+end:start-end;

return(NumTicksElasped/295000);
}

//Create a new thread.
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
{
Expand Down Expand Up @@ -280,10 +273,10 @@ static u32_t sys_arch_mbox_fetch_internal(sys_mbox_t pMBox, void** ppvMSG, u32_t
TimeElasped=0;
if(block){
int start;
start=clock()/(CLOCKS_PER_SEC/1000);
start=clock();

if((result=ReceiveMbx(&pmsg, pMBox, u32Timeout))==0){
TimeElasped = ComputeTimeDiff(start, clock()/(CLOCKS_PER_SEC/1000));
TimeElasped = (clock() - start) / (1000);
}
else{
return SYS_ARCH_TIMEOUT;
Expand Down Expand Up @@ -392,10 +385,10 @@ u32_t sys_arch_sem_wait(sys_sem_t *sema, u32_t timeout)
unsigned int start;
u32_t WaitTime;

start=clock()/(CLOCKS_PER_SEC/1000);
start=clock();
if(WaitSemaTimeout(*sema, timeout) == *sema)
{
WaitTime=ComputeTimeDiff(start, clock()/(CLOCKS_PER_SEC/1000));
WaitTime=(clock()-start)/(1000);
result=(WaitTime <= timeout ? WaitTime : timeout);
}
else result=SYS_ARCH_TIMEOUT;
Expand Down Expand Up @@ -450,7 +443,7 @@ void sys_init(void)

u32_t sys_now(void)
{
return(clock()/(CLOCKS_PER_SEC/1000));
return(clock()/1000);
}

sys_prot_t sys_arch_protect(void)
Expand Down