- 
                Notifications
    
You must be signed in to change notification settings  - Fork 28
 
Rp2040 multithreaded target support #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 12 commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9d2a3d2
              
                initial draft for multithreaded rp2040
              
              
                sberkun 7784f3d
              
                add PLATFORM_RP2040 cmake variable
              
              
                sberkun 754c5d0
              
                allow core 1 to sleep
              
              
                sberkun 488afdd
              
                mutex to recursive mutex, check return values of sem_release
              
              
                sberkun bcd8a2b
              
                fixed bug with recursive mutex
              
              
                sberkun 46ed5c8
              
                cleanup; atomics were seperated last week
              
              
                sberkun 82f42e1
              
                rename _lf_cond_timedwait
              
              
                sberkun 5d1f1a3
              
                move thingy
              
              
                sberkun f34d0c6
              
                peter...
              
              
                sberkun e00d73a
              
                ???
              
              
                sberkun 3791826
              
                fix compile errors
              
              
                sberkun 331cc91
              
                make cond_broadcast from interrupts sound
              
              
                sberkun b58a3a5
              
                Ran formatter
              
              
                edwardalee File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -31,13 +31,10 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| * @author{Abhi Gundrala <[email protected]>} | ||
| */ | ||
| 
     | 
||
| #if !defined(LF_SINGLE_THREADED) | ||
| #error "Only the single-threaded runtime has support for RP2040" | ||
| #endif | ||
| 
     | 
||
| 
     | 
||
| #include "platform/lf_rp2040_support.h" | ||
| #include "low_level_platform.h" | ||
| #include "utils/util.h" | ||
| #include "tag.h" | ||
| 
     | 
||
| #include <pico/stdlib.h> | ||
| 
          
            
          
           | 
    @@ -67,6 +64,8 @@ static uint32_t _lf_num_nested_crit_sec = 0; | |
| */ | ||
| void _lf_initialize_clock(void) { | ||
| // init stdio lib | ||
| // may fail, but failure may be ok/expected if printing is not needed | ||
| // (i.e. if neither USB nor UART are enabled) | ||
| stdio_init_all(); | ||
| // init sync structs | ||
| critical_section_init(&_lf_crit_sec); | ||
| 
          
            
          
           | 
    @@ -161,7 +160,7 @@ int lf_disable_interrupts_nested() { | |
| return 1; | ||
| } | ||
| // check crit sec count | ||
| // enter non-rentrant state by disabling interrupts | ||
| // enter non-reentrant state by disabling interrupts | ||
| // lock second core execution | ||
| if (_lf_num_nested_crit_sec == 0) { | ||
| // block if associated spin lock in use | ||
| 
          
            
          
           | 
    @@ -202,9 +201,125 @@ int lf_enable_interrupts_nested() { | |
| */ | ||
| int _lf_single_threaded_notify_of_event() { | ||
| // notify main sleep loop of event | ||
| sem_release(&_lf_sem_irq_event); | ||
| if (sem_release(&_lf_sem_irq_event)) { | ||
| return 0; | ||
| } | ||
| return 1; | ||
| } | ||
| 
     | 
||
| #else // LF_SINGLE_THREADED | ||
| 
     | 
||
| #warning "Threaded runtime on RP2040 is still experimental" | ||
| 
     | 
||
| /** | ||
| * @brief Get the number of cores on the host machine. | ||
| */ | ||
| int lf_available_cores() { | ||
| return 2; | ||
| } | ||
| 
     | 
||
| static void *(*thread_1) (void *); | ||
| static void* thread_1_args; | ||
| static int num_create_threads_called = 0; | ||
| static semaphore_t thread_1_done; | ||
| static void* thread_1_return; | ||
| 
     | 
||
| #define MAGIC_THREAD1_ID 314159 | ||
| 
     | 
||
| void core1_entry() { | ||
| thread_1_return = thread_1(thread_1_args); | ||
| sem_reset(&thread_1_done, 1); | ||
| } | ||
| 
     | 
||
| int lf_thread_create(lf_thread_t* thread, void *(*lf_thread) (void *), void* arguments) { | ||
| // make sure this fn is only called once | ||
| if (num_create_threads_called != 0) { | ||
| return 1; | ||
| } | ||
| thread_1 = lf_thread; | ||
| thread_1_args = arguments; | ||
| num_create_threads_called += 1; | ||
| sem_init(&thread_1_done, 0, 1); | ||
| multicore_launch_core1(core1_entry); | ||
| *thread = MAGIC_THREAD1_ID; | ||
| return 0; | ||
| } | ||
| #endif // LF_SINGLE_THREADED | ||
| 
     | 
||
| int lf_thread_join(lf_thread_t thread, void** thread_return) { | ||
| if (thread != MAGIC_THREAD1_ID) { | ||
                
      
                  sberkun marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| return 1; | ||
| } | ||
| sem_acquire_blocking(&thread_1_done); | ||
| // release in case join is called again | ||
| if (!sem_release(&thread_1_done)) { | ||
| // shouldn't be possible; lf_thread_join is only called from main thread | ||
| return 1; | ||
| } | ||
| if (thread_return) { | ||
| *thread_return = thread_1_return; | ||
| } | ||
| return 0; | ||
| } | ||
| 
     | 
||
| int lf_mutex_init(lf_mutex_t* mutex) { | ||
| recursive_mutex_init(mutex); | ||
| return 0; | ||
| } | ||
| 
     | 
||
| int lf_mutex_lock(lf_mutex_t* mutex) { | ||
| recursive_mutex_enter_blocking(mutex); | ||
| return 0; | ||
| } | ||
| 
     | 
||
| int lf_mutex_unlock(lf_mutex_t* mutex) { | ||
| recursive_mutex_exit(mutex); | ||
| return 0; | ||
| } | ||
| 
     | 
||
| // condition variables "notify" threads using a semaphore per core. | ||
| // although there are only two cores, may not use just a single semaphore | ||
| // as a cond_broadcast may be called from within an interrupt | ||
| int lf_cond_init(lf_cond_t* cond, lf_mutex_t* mutex) { | ||
| for (int i = 0; i < NUM_CORES; i++) { | ||
| sem_init(&(cond->notifs[i]), 0, 1); | ||
| } | ||
| cond->mutex = mutex; | ||
| return 0; | ||
| } | ||
| 
     | 
||
| int lf_cond_broadcast(lf_cond_t* cond) { | ||
| for (int i = 0; i < NUM_CORES; i++) { | ||
| sem_reset(&(cond->notifs[i]), 1); | ||
                
      
                  sberkun marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| return 0; | ||
| } | ||
| 
     | 
||
| int lf_cond_signal(lf_cond_t* cond) { | ||
| return lf_cond_broadcast(cond); // spurious wakeups, but that's ok | ||
                
      
                  sberkun marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| 
     | 
||
| int lf_cond_wait(lf_cond_t* cond) { | ||
| semaphore_t* mailbox = &(cond->notifs[get_core_num()]); | ||
| lf_mutex_unlock(cond->mutex); | ||
| sem_acquire_blocking(mailbox); | ||
| lf_mutex_lock(cond->mutex); | ||
| return 0; | ||
| } | ||
| 
     | 
||
| int _lf_cond_timedwait(lf_cond_t* cond, instant_t absolute_time_ns) { | ||
| semaphore_t* mailbox = &(cond->notifs[get_core_num()]); | ||
| absolute_time_t a = from_us_since_boot(absolute_time_ns / 1000); | ||
| bool acquired_permit = sem_acquire_block_until(mailbox, a); | ||
| return acquired_permit ? 0 : LF_TIMEOUT; | ||
| } | ||
| 
     | 
||
| void initialize_lf_thread_id() {} | ||
| 
     | 
||
| int lf_thread_id() { | ||
| return get_core_num(); | ||
| } | ||
| 
     | 
||
| 
     | 
||
| #endif // !LF_SINGLE_THREADED | ||
| 
     | 
||
| #endif // PLATFORM_RP2040 | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.