<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Best way to share system state data between tasks? in MQX Software Solutions</title>
    <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393096#M13154</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't suppose you have a good example of code creating a shared memory space, defining a structure in it, then sharing it with another task, and that task accessing the structure? I want to make sure I don't screw something up when it comes to creating the memory block and passing around pointers to it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 30 Apr 2015 20:38:14 GMT</pubDate>
    <dc:creator>robertyork</dc:creator>
    <dc:date>2015-04-30T20:38:14Z</dc:date>
    <item>
      <title>Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393090#M13148</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've got a project which has several tasks running and an overall system state I want to track (things like standby mode, whether a toggle state is in one mode or another). Normally, I would have a structure somewhere and use get and set methods to read and write to the state. With multiple tasks, it's not so simple. It seems like in MQX, there's a few ways to tackle this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The one that immediately comes to mind is that I could send messages to get and set the state, but that becomes either a lot of messages, or passing a large state structure as a message. This is especially cumbersome if I have one task managing the state. It seems like I would have to send a message to make a change, the when another task wants to read a state, it has to send a message requesting it, then wait for the state to get shipped back. Seems like a lot of overhead. Is there something a little simpler? Perhaps sending a message when the state changes, some task updating the state somewhere, then the state is visible to all other tasks through some shared memory or a simple get_state() function call. Any thoughts or ideas here would be appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The overall concept is a task that is in charge of managing the state (so only one task can write) but anyone can read it. Seems this should be a fairly common problem, and I'm sure there's some sort of design pattern that fits this with little overhead in MQX.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 14:48:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393090#M13148</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T14:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393091#M13149</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;I would suggest using lwevents.&lt;/P&gt;&lt;P&gt;Probable to the intended use case for it but you have the ability to set/clear any of the bits in the 32-bit (4-byte) field so you can enumerate states for the tasks and update that lwevent to indicate whatever.&lt;/P&gt;&lt;P&gt;I played around with the lwevent.c example in the C:\Freescale\Freescale_MQX_4_1_1\mqx\examples\lwevent folder and attached it here for reference.&lt;/P&gt;&lt;P&gt;I'm only using one bit of the lwevent and it gets set in the ISR and cleared in the task waiting for the flag to set.&amp;nbsp; I added a task to monitor the lwevent flag too.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 17:45:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393091#M13149</guid>
      <dc:creator>DavidS</dc:creator>
      <dc:date>2015-04-30T17:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393092#M13150</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wouldn't using a lightweight semaphore do the trick for you as well?&amp;nbsp; You can use OSA_SemaWait to get access to the shared memory, then call OSA_SemaPost afterward so the other tasks can access it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 19:23:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393092#M13150</guid>
      <dc:creator>dave408</dc:creator>
      <dc:date>2015-04-30T19:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393093#M13151</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I hadn't thought of using an event flag. I currently use one to synchronize that all my tasks have initialized message queues before they start going off and posting messages to each other. I have probably 20-30 (depending how I structure things) different variables I want to watch. Originally I'd use a structure for them, but most of them are really Boolean states, so an event flag might work, with each bit representing one of them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I may want to store time stamps for when each of those flags changed, in which case the message system sounded more appealing. I also don't want anything waiting on this 'event', I just need to check what state it's in. &lt;SPAN style="font-size: 13.3333330154419px; line-height: 1.5em;"&gt; &lt;/SPAN&gt;&lt;SPAN style="font-size: 13.3333330154419px; line-height: 1.5em;"&gt;I'll have to think about the idea of using this. It would certainly be a simple approach. And I like simple.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:02:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393093#M13151</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T20:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393094#M13152</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm still learning MQX, so I'm not yet familiar with how to do shared memory in it. I'm familiar with the concepts, of course, and I could certainly use a semaphore to lock down a memory address somewhere. My question is, should I need to lock memory if only one task can write to it? I suppose it would make sure the data read is valid and not only half-valid. I'll need to find an example of something implementing shared memory and sharing data between tasks that way. I'm not sure how MQX handles creating a shared memory space, other than I know there's some sort of MQX malloc().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:22:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393094#M13152</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T20:22:33Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393095#M13153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm in the same boat.&amp;nbsp; I would think you only need to lock shared memory if the write operation takes more than one instruction.&amp;nbsp; In your case, maybe you can get away with it.&amp;nbsp; To be safe, the way I would do it is to brute force it and write code that reads and writes the struct, then generate the assembly code and look at how many ops it takes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As far as shared memory goes, if you are okay with it, MQX will let you use a global variable, or you could pass the shared memory structure to your task as an argument.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:31:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393095#M13153</guid>
      <dc:creator>dave408</dc:creator>
      <dc:date>2015-04-30T20:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393096#M13154</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't suppose you have a good example of code creating a shared memory space, defining a structure in it, then sharing it with another task, and that task accessing the structure? I want to make sure I don't screw something up when it comes to creating the memory block and passing around pointers to it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:38:14 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393096#M13154</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T20:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393097#M13155</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't at the moment, but creating the "shared memory" is literally defining your variable for the structure in your os_tasks.c file, and then all of your tasks can directly modify it because it's file scope.&amp;nbsp; You then create the semaphore with OSA_SemaCreate(), lock it with OSA_SemaWait(), and release it with OSA_SemaPost().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:41:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393097#M13155</guid>
      <dc:creator>dave408</dc:creator>
      <dc:date>2015-04-30T20:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393098#M13156</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;Okay, now that I'm thinking about this a little more, I was thinking exactly that.&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;On scope, I have a main.c which only has a TASK_TEMPLATE_STRUCT. Is that .c file (and variables declared within) in scope for all those tasks defined there? Sorry if this seems like a silly question. Part of me wants to think it is, being in main.c, but the other part isn't not sure, with all of the context switches that occur with multi-tasking. I've been bitten by scope problems and multitasking before.&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;For some reason I was thinking I needed dynamic memory allocation, but I really shouldn't. This seems like a lot easier.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:49:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393098#M13156</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T20:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393099#M13157</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;While I'm thinking about it, why do you suggest a semaphore over a mutex? Perhaps a bit off topic, but I would normally think a mutex would be more appropriate. However, it seems MQX implements them differently, from what I've read. Just trying to understand this better.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 21:09:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393099#M13157</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T21:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393100#M13158</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;WRT not wanting to wait on event,you don't need to call the _lwevent_wait_XXX if you are just looking to see what that VALUE is set to. &lt;/P&gt;&lt;P&gt;_lwevent_set/clear are none blocking but do run the scheduler so if another higher priority task came available it would switch to it.&amp;nbsp; But usually that is a good thing :-).&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;David &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 21:23:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393100#M13158</guid>
      <dc:creator>DavidS</dc:creator>
      <dc:date>2015-04-30T21:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393101#M13159</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;The variables declared in your main.c would be global variable and not something using the dynamic heap.&amp;nbsp; The other tasks could see the global variables.&lt;/P&gt;&lt;P&gt;If task in a separate file then extern declaration needed.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;David &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 21:33:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393101#M13159</guid>
      <dc:creator>DavidS</dc:creator>
      <dc:date>2015-04-30T21:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393102#M13160</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;For MQX the mutex has added features that semaphore does not.&amp;nbsp; Like controls for priority inversion.&lt;/P&gt;&lt;P&gt;A mutex basically is a semaphore with one key.&amp;nbsp; Or a semaphore with one key is like a mutex but doesn't have option for priority inversion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Look at MQX_User_Guide.pdf in the MQX_4.1.1/docs/mqx folder:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="ScreenHunter_141 Apr. 30 16.38.gif"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/51359i8E1E05858A62FD2F/image-size/large?v=v2&amp;amp;px=999" role="button" title="ScreenHunter_141 Apr. 30 16.38.gif" alt="ScreenHunter_141 Apr. 30 16.38.gif" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 21:38:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393102#M13160</guid>
      <dc:creator>DavidS</dc:creator>
      <dc:date>2015-04-30T21:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393103#M13161</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would like to avoid dynamic memory allocation. So a static global that anyone can read, then locking it down with mutex/semaphore, I believe, would be the best way to do it. I assume I could then also make some get/set methods around it and treat it like I have in the past.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'll give this a try and see how things work. Doesn't sound too tough.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 21:41:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393103#M13161</guid>
      <dc:creator>robertyork</dc:creator>
      <dc:date>2015-04-30T21:41:55Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to share system state data between tasks?</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393104#M13162</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;one think to consider while selecting between LWsem, Semaphore or Mutex is code size and speed as mentioned by David.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For your case (accessing a structure with state data from multiple tasks) I would suggest to use&amp;nbsp; LWsem to protect consistency of data. Functionality it provides is sufficient for you use case and gives you fastest response and lower memory footprint.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Petr&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 21:54:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Best-way-to-share-system-state-data-between-tasks/m-p/393104#M13162</guid>
      <dc:creator>PetrL</dc:creator>
      <dc:date>2015-04-30T21:54:09Z</dc:date>
    </item>
  </channel>
</rss>

