lib/thread: Add mutex
We need a way to protect shared resources. Since we are using cooperative multitasking the mutex implementation is pretty trivial. BUG=b:179699789 TEST=Verify thread lock and unlock. Signed-off-by: Raul E Rangel <rrangel@chromium.org> Change-Id: Ife1ac95ec064ebcdd00fcaacec37a06ac52885ff Reviewed-on: https://review.coreboot.org/c/coreboot/+/56230 Reviewed-by: Karthik Ramasubramanian <kramasub@google.com> Reviewed-by: Furquan Shaikh <furquan@google.com> Reviewed-by: Julius Werner <jwerner@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
		
				
					committed by
					
						
						Raul Rangel
					
				
			
			
				
	
			
			
			
						parent
						
							a98d302fe9
						
					
				
				
					commit
					b29f9d471b
				
			@@ -1,5 +1,6 @@
 | 
			
		||||
/* SPDX-License-Identifier: GPL-2.0-only */
 | 
			
		||||
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
@@ -377,3 +378,22 @@ void thread_coop_disable(void)
 | 
			
		||||
 | 
			
		||||
	current->can_yield--;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void thread_mutex_lock(struct thread_mutex *mutex)
 | 
			
		||||
{
 | 
			
		||||
	struct stopwatch sw;
 | 
			
		||||
 | 
			
		||||
	stopwatch_init(&sw);
 | 
			
		||||
 | 
			
		||||
	while (mutex->locked)
 | 
			
		||||
		assert(thread_yield() == 0);
 | 
			
		||||
	mutex->locked = true;
 | 
			
		||||
 | 
			
		||||
	printk(BIOS_SPEW, "took %lu us to acquire mutex\n", stopwatch_duration_usecs(&sw));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void thread_mutex_unlock(struct thread_mutex *mutex)
 | 
			
		||||
{
 | 
			
		||||
	assert(mutex->locked);
 | 
			
		||||
	mutex->locked = 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user