This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Socket manager

Hello,

Multiple threads on our board require network connections, which can be over either the LTE-M modem, or over ethernet.

To this end, we created a Socket Manager thread. This receives requests from the other threads and should open and close sockets, write to them, and forward any read data to the owning thread.

This leads to 2 challenges:
- What is the best way to monitor the active sockets in parallel, so that we can notify the owning threads when data / events arive?
- How do we also support the requests coming in at the same time?

Ideas:
- Socket Manager thread alternates between checking the request Message Queue and polling the sockets (this seems ugly)
- Socket Manager thread spawn threads for every new socket (then, how do we dynamically allocate and start threads?)

Feedback or better ideas are greatly appreciated!

Parents Reply Children
  • Hi again Carl!

    I figured that yesterday was more about the higher level whereas this post was more directed to sockets specifically, hence the new post. I hope that's okay.

    I was indeed curious whether thread stacks could be allocated dynamically, but if I understand correctly the answer is no. This means I should initialize 8 socket handler thread stacks beforehand (as the socket API supports 8 sockets I believe), even though I might not actually use all 8.

  • Hi!

    That makes sense and of course it's okay! 

    I did some more digging in the documentation and it seems that it may be possible after all. Take a look here. I'm not totally sure yet, though and will test it, but that will probably not be finished before noon tomorrow.

    Best regards,
    Carl Richard

  • Hi Carl,

    Thank you for your response, the extra information, and for taking the time to test it. I also saw the page you linked, but for k_object_alloc() it says "Currently, allocation of thread stacks is not supported."

    Some more googling led me to CONFIG_DYNAMIC_OBJECTS, which I enabled and did not get an error on yet. I'll let you know if I have any luck.

  • Hi Carl,

    Some quick experimentation proved that I should not always believe the Zephyr documentation:

    k_thread_stack_t *testStacks[8];
    static struct k_thread testThreads[8];
    
    K_THREAD_STACK_DEFINE(networkingThreadStack, 2024);
    struct k_thread networkingThread;
    
    
    
    static void TestThreadFxn (const uint8_t *pId)
    {
      printk("Created thread %u\n", *pId);
    }
    
    static void NetworkingThreadFxn (void)
    {
      for (uint8_t i = 0; i < 8; i++)
      {
        testStacks[i] = k_malloc(1024);
        uint8_t *pId = k_malloc(sizeof(uint8_t));
        *pId = i;
    
        k_thread_create(&testThreads[i], testStacks[i],
                        1024,
                        (k_thread_entry_t) TestThreadFxn,
                        pId, NULL, NULL,
                        NETWORKING_THREAD_PRIORITY, 0, K_NO_WAIT);
      }
    }
    
    void NetworkingCreateThread (void)
    {
      k_thread_create(&networkingThread, networkingThreadStack,
                      K_THREAD_STACK_SIZEOF(networkingThreadStack),
                      (k_thread_entry_t) NetworkingThreadFxn,
                      NULL, NULL, NULL,
                      NETWORKING_THREAD_PRIORITY, 0, K_NO_WAIT);
    }

    Created thread 0
    Created thread 1
    Created thread 2
    Created thread 3
    Created thread 4
    Created thread 5
    Created thread 6
    Created thread 7

    No crashes, as long as I allocate enough memory as stack.

Related