New Features of cthread and libc - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

New Features of cthread and libc

Description:

New Features of cthread and libc. Weirong Zhu. Juan del Cuvillo. Outline ... Cthread ... (int argc, char **argv) cthread_t thread; printf('Entering ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 29
Provided by: capsl
Category:
Tags: argv | cthread | features | libc | new

less

Transcript and Presenter's Notes

Title: New Features of cthread and libc


1
New Features of cthread and libc
  • Weirong Zhu
  • Juan del Cuvillo

2
Outline
  • Introduction of Cthread
  • Current Cthread library
  • New Features of cthreads
  • New Features of libc

3
Introduction
  • Cthread Cyclops Thread Library
  • The first implementation of our PXM based on a
    Pthread-like model
  • Direct mapping of software threads into hardware
    thread units
  • Fast thread creation and reuse
  • Non-preemptive thread execution

4
CThread Management
  • cthread_create()
  • Runs the user provide function in next
    available thread unit. If no thread unit is
    available, returns an error condition.
  • cthread_exit()
  • Caller thread terminates its execution.
  • cthread_join()
  • Wait for the target thread to terminate.

5
CThread Management
  • cthread_self()
  • Returns own thread ID
  • cthread_sleep()
  • Caller thread is suspended for a given number
    of clock cycles
  • cthread_resume()
  • Awakes a suspended thread
  • cthread_sync()
  • Waits for all accesses to memory, initiated by
    the calling thread, to be completed in memory

6
Mutex Management
  • cthread_mutex_init()
  • Initialize a mutex object.
  • cthread_mutex_destroy()
  • Destroys a mutex object.
  • cthread_mutex_lock()
  • Locks a mutex. If already locked, block until
    available.
  • cthread_mutex_trylock()
  • Locks a mutex. If locked, returns immediately.
  • cthread_mutex_unlock
  • unlocks a mutex.

7
New Features in cthread
  • Condition Variables
  • One-time initialization
  • Thread-Specific Data
  • Thread Cancellation
  • others

8
  • Condition Variables

9
Motivations
  • While mutexes implement synchronization by
    controlling thread access to data, condition
    variables allow threads to synchronize based upon
    the actual value of data.
  • Provides a notification system among threads
  • Without condition variables, threads need to
    continually polling the variable to check if the
    condition is met. This can be very resource
    consuming, like generating traffic in the memory
    bus. A condition variable is a way to achieve the
    same goal without polling.
  • A condition variable is always used in
    conjunction with a mutex lock. (Condition
    variables are usually associated with a predicate
    which indicates the state. A thread which waits
    on a condition variable should test the predicate
    within the exclusion zone provided by the
    associated mutex to determine the resource state.)

10
Example
11
A representative sequence for using condition
variables
12
Cthread routines for using condition variable
  • cthread_cond_init()
  • Initializes a condition variable.
  • cthread_cond_destroy()
  • Deletes a condition variable.
  • cthread_cond_signal()
  • Unblocks one of the potentially many threads
    blocked on a condition.
  • cthread_cond_broadcast()
  • Unblocks all threads blocked on a condition.

13
Cthread routines for using condition variable
  • cthread_cond_wait()
  • Suspends the caller until another thread
    signals on the condition variable.
  • cthread_cond_timewait()
  • Specifies a time argument. If the condition is
    not signaled in the specified time, the thread is
    released from its wait.

14
  • One-time initialization

15
Motivations
  • Defer the initialization of resources until a
    thread requires them.
  • Application may require that multiple threads use
    a certain resource, which requires to initialize
    the resource only once in a threadsafe way.
  • cthread_once mechanism is the tool of choice for
    this situation.
  • cthread_once handles synchronization among
    threads at initialization time.

16
Example
Output Enter Testcase Create/start threads
Wait for the threads to complete, and release
their resources Thread 1 Entered Thread 0
Entered Thread 3 Entered Thread 2 Entered
Thread 4 Entered Thread 1 INITIALIZE RESOURCE
Thread 1 The resource is 42 Thread 0 The
resource is 42 Thread 3 The resource is 42
Thread 2 The resource is 42 Thread 4 The
resource is 42 Main completed
17
One-time initialization
  • cthread_once(cthread_once_t once_control, void
    (init_routine)(void))
  • The cthread_once() function performs one time
    initialization based on a specific once_control
    variable. The init_routine is called only one
    time when multiple calls to cthread_once() use
    the same once_control.

18
  • Thread-specific Data

19
Motivations
  • Typical applications that are not threaded use
    global storage. When changing the application to
    support multithreading, synchronization technique
    must be used to protect global storage from being
    changed by multiple threads at the same time.
    Thread-specific data allows a thread to maintain
    its own global storage that is hidden from the
    other threads.
  • Multi-threaded application are hard to program
    due to the complex concurrency control protocols
    required to avoid race conditions and deadlocks.
    Thread-specific data simplifies the programming
  • Thread-specific data gives compiler and RTS the
    hints to optimize the memory locality

20
Simple Example
21
Features
  • Allow multiple threads to use one logically
    global access point (the key) to retrive
    thread-specific data.
  • Allow a thread to maintain its own global storage
    that is hidden from the other threads.
  • Suitable for multi-threaded applications that
    frequently access objects that are logically
    global but physically specific to each thread.
  • Allow sequential function calls within a thread
    to access thread-specific data atomically. Don't
    require caller to provide context in interface
    arguments, don't burden the caller with
    maintaining this type of context in global
    environment

22
Thread-specific Data
  • cthread_key_create()
  • creates a thread local storage key for the
    process, that key can be used to set and get
    per-thread data pointer
  • cthread_key_delete()
  • deletes a process-wide thread local storage
    key
  • cthread_setspecific()
  • sets the thread local storage value associated
    with a key
  • cthread_getspecific()
  • retrieves the thread local storage value
    associated with the key

23
  • Thread Cancellation

24
Motivations
  • Cancellation allows one thread to terminate
    another.
  • Save system resources, when program determines
    that the thread's activity is no longer necessary
  • A very rough synchronization mechanism
  • cthread_cancel(cthread_t thread), cancel the
    specified thread.

25
Example
26
Others
  • cthread_equal (cthread_t t1, cthread_t t2)
  • The cthread_equal subroutine compares the
    thread IDs thread1 and thread2. Since the thread
    IDs are opaque objects, it should not be assumed
    that they can be compared using the equality
    operator().

27
Standard C library (libc)
  • File I/O
  • open, close, read, write, etc.
  • Uniform access across all target platforms
    simulator, board, and system.
  • Modify _open, _close, _read, _write system calls.
  • Status proof of concept under testing for printf.

28
Standard C library (cont.)
  • Memory management
  • malloc, heap, etc.
  • Atom table interface should rely on them.
  • Modify _sbrk system call.
  • Status under testing.
Write a Comment
User Comments (0)
About PowerShow.com