0

The following NASM code uses pthread_cond_wait to take 4 cores (numbered 0, 8, 16, 24) in core order, starting with core 0. After each core locks the mutex, it enters mutex_lock_loop where it tests for core number, which is stored in rbx. If the core number equals the variable "wait_core" then it unlocks the mutex and executes a small block of code (see TEMP FOR TESTING), then increments the variable wait_core (by 8 to match the core numbers).

; Mutex wait vars
mutex_01: dq 0
count_mutex: dq 0
mutex_01_attr: dq 0
wait_condtion: dq 0
wait_condition_attr: dq 0
wait_core: dq 0
; _____________

mov rdi,mutex_01
mov rsi,mutex_01_attr
call pthread_cond_init wrt ..plt

mov rdi,count_mutex
mov rsi,mutex_01_attr
call pthread_cond_init wrt ..plt

; Initialize wait_condtion
mov rdi,wait_condtion
mov rsi,wait_condition_attr
call pthread_cond_init wrt ..plt
;_____________

mov rdi,mutex_01
call pthread_mutex_lock wrt ..plt

mutex_lock_loop:
mov rdi,wait_condtion
mov rsi,mutex_01
call pthread_cond_wait wrt ..plt
mov rax,[wait_core]
cmp rax,rbx
jne mutex_lock_loop

mov rdi,mutex_01
call pthread_mutex_unlock wrt ..plt

mov rdi,count_mutex
call pthread_mutex_lock wrt ..plt

mov rax,rbx ; TEMP FOR TESTING
add rax,5 ; TEMP FOR TESTING
mov rdi,audit_array ; TEMP FOR TESTING
mov [rdi+rbx],rax ; TEMP FOR TESTING

mov rax,[wait_core]
add rax,8
cmp rax,[Number_Of_Cores]
jge core_out
mov [wait_core],rax

mov rdi,count_mutex
call pthread_mutex_unlock wrt ..plt

core_out:

My research has not turned up any assembly language examples for pthread_cond_wait, so I modelled this on C code from research:

void *functionCount1() { for(;;) { pthread_mutex_lock( &condition_mutex );

        while( count >= COUNT_HALT1 && count <= COUNT_HALT2 )
        {
        pthread_cond_wait( &condition_cond, &condition_mutex );
        }

        pthread_mutex_unlock( &condition_mutex );

        pthread_mutex_lock( &count_mutex );
        count++;
        printf("Counter value functionCount1: %d\n",count);
        pthread_mutex_unlock( &count_mutex );

        if(count >= COUNT_DONE) return(NULL);
    }
}

The code as written hangs, which means the condition loop is not working. My problem may be that the mutexes and condition vars are initialized in C with PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER, whereas here they are initialized to zero.

I know this is not a minimal, complete and reproducible example. It comes from a much larger code base. I hoped that someone with experience in pthread_condition_wait could spot my problem based on the code above. I know that the mutexes and condition vars are being created correctly based on their return codes.

This is my first work with pthread_cond_wait. Thanks very much for any help with this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.