Taylor Huang
Taylor Huang

Reputation: 61

how to use when a lib function has the same name with it's system call?

code as follows, sched_yield() has the same name of lib function and system call, I don't know sched_yield() in the code is a lib function or a system call, how to distinguish?

#include sched.h

void shm_lock(volatile uint32_t *lock, uint32_t value, uint32_t spin)
{
    uint32_t  i, n, pid;

    for ( ;; ) {
        if (*lock == 0 && cmp_set(lock, 0, value)) {
            return;
        }

        for (n = 1; n < spin; n <<= 1) {
            for (i = 0; i < n; i++) {
                //printf("i %d\n",i);
                asm_cpu_pause();
            }
            //printf("n %d\n",n);
            if (*lock == 0 && cmp_set(lock, 0, value)) {
                return;
            }
        }

        sched_yield(); // here!!!

        pid = *lock;
        if(pid){
            if(kill(pid,0)==-1 && errno == ESRCH){   
                cmp_set(lock, pid, 0);
            }
        }
    }
}

Upvotes: 2

Views: 381

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60068

If it has the same name as a system call, then it's usually your standard library's wrapper around that system call.

The musl libc standard library defines sched_yield() as:

#include <sched.h>
#include "syscall.h"

int sched_yield()
{
    return syscall(SYS_sched_yield);
}

(A most straightforward syscall wrapper).

There's no notational difference between function calls and system call wrappers in C if that's what you're asking.

System call wrappers are just functions (could be macros) that (usually) call the syscall function, which lays out its parameters to the appropriate registers and invokes the proper instruction for your architecture (int 80h on x86 for instance) to signal the kernel to take it from there.

Upvotes: 3

Related Questions