sem_timedwait is applying duplicate locks

I have written a sample C program to call multiple threads in a process. These multiple threads access the same semaphore handle and tries to apply lock with sem_timedwait(). If the lock is already applied then it should wait for 3 seconds for the lock to get released. If the lock is not released even after 3 seconds then return failure.



snprintf(semaphore_name,10,"Sem_lock");
sem_handle = sem_open(semaphore_name, O_CREAT, S_IRUSR|S_IWUSR, 1);
if(sem_handle == SEM_FAILED){
       return FAIL;
}
gettimeofday(&time_now,NULL);
tm.tv_sec = time_now.tv_sec;
tm.tv_sec += 3;
tm.tv_nsec = time_now.tv_usec;
tm.tv_nsec = tm.tv_nsec*1000;

if(sem_timedwait(sem_handle,&tm) == -1){
    return FAIL;
}


I could see sem_timedwait is simultaneously applying locks for multiple threads for the same semaphore handle.