Search Forums by tag:
Serial, mbed, compiler, ethernet, USB, I2C, SPI, interrupt, LCD, library, bug, HTTPServer, CAN, AnalogIn, adc, Power, Ticker, memory, pwm, SD Card, InterruptIn, rpc, Error, SDFileSystem, PwmOut, LocalFileSystem, UART, canbus, driver, TCP, interrupts, rtos, led, libraries, editor, timer, accelerometer, GPS, file, clock, website, C++, SD, frequency, reset, http, LPC11U24, flash, SDCard, RTC, DigitalIn, TCPSocket, problem, printf, Java, Servo, buffer, UDP, SerialPC, DMA, HTTPClient, Sleep, audio, pinball, MODSERIAL, NetServices, socket, array, compile, filesystem, RFID, beta, m3pi, write, LPC1768, multiple, newbie, keyboard, sensor, GPRS, Forum, digitalOut, assembly, debug, hardware, Speed, xbee, AnalogOut, RPCFunction, EthernetNetIf, Download, code, voltage, wait, network, C, suggestion, JTAG, keil, MATLAB, offline, Board, lwip, I2S, dead, Nokia6610, time, bluetooth, WiFly, current, tcp/ip, MODDMA, SPI Slave, pololu, robot, Communication, read, dac, string, pc, binary, filter, copy, USB Host, publish, rs232, DHCP, Host, Data Logging, windows, firmware, malloc, mp3, PCB, gcc, attach, program, fatfilesystem, class, email, arduino, stepper motor, WavePlayer, wifi, Nokia, camera, size, VGA, import, documentation, ide, linux, baud, TextLCD, Cortex-M0, M0, pointers, pullup, Relay, timing, function, latency, serial port, MIDI, compiler error codes, magjack, touch, screen, Production, client, server, stream, HID, breakout, FIFO, prototype, flashing, GPIO, sampling, Analog, display, api, ADXL345, Encoder, DSP, help, motor, sram, suggestions, PING, Terminal, link, browser, Pin, control, Eagle, Modbus, EEPROM, mac, Timeout, fopen, port, updates, usbserial, batteries, DMX, files, USBMIDI, scanf, protocol, PPP, slave, FTP, integer, noise, MODGPS, modem, float, threads, motors, for, monitor, Digital I/O, 7, Windows Serial Driver, pins, keypad, FAT, classes, webserver, delay, variables, time-triggered, c programming, labview, watchdog, post, math, Battery, LPCXpresso, MBED website, GSM, storage, nxp, mobileLCD, license, int, counter, baseboard, Assembler, Vin, rj45, registers, E289, news, i2cmaster, amoled, Compiling, connect, revision, prototype to hardware, UMTSStick, Optocoupler, Robotics, search, oscillator, glitch, Websockets, load, find, real-time, routine, format, offline compile, powersource, driverlibrary, processing, networking, ID, umts, debugging, color, BUTTON, software, PS3, Images, wave, bin, const, SNMP, OSX, supply, peripheral, sensors, data, Design, PID, version, RIT, character, freeze, USBDevice, bus, ARM, wav, SRF08, heap, output, basic, TFT, QVGA, mysql, piezo, update, ID12, Pachube, player, DigitalInOut, object, cmsis, capture, IR, slow, 1768, PSP, OS, syntax, mbed.lib, EmbeddedArtists, NMEA, paste, project, Web, GUI, UART0, firefox, SQL, wakeup, RAM, bitmap, handler, security, 3D, bugs, OLED, Temperature, not, I/O, Bidirectional, rss, wireless, delete, resolved, LED1, LIS302, getc, Safari, Wi-Fi, wiki, PinNames, accounts, PS2, BusOut, projects, RS485, python
Stefan Wilhelm
critical_section,
dint,
eint,
interrupt
|
2 replies
You can use __disable_irq()/__enable_irq(). Howewer, it might be better to not do something that drastic just for synchronization... try my semaphore class that uses exclusive load instructions.
Please log in to post a reply.
Hi all && mbed Team, I wrote a class fifo<typename T_ Tsize_>, which has to be synchronised as it could be read/written in the main loop as well as in a e.g. Serial receive interrupt. (I would like to use parts of my code on the mbed as well as the host unix/win system). How can I disable / enable the interrupts (EINT / DINT) in order to get that stuff done || is there already a class like critical_section? Thanks very much for your help ;) Cheers, Stefan ------------------------------------------------------ I thought of something probably looking like this. (Methods are const and instance variables hence mutable to enable to lock/unlock in const methods that are using synchronisation.) What I'm missing is eint() and dint().namespace sw { namespace sync { class critical_section { public: #if defined(OS_UNIX) critical_section() { pthread_mutex_init(&_cs, NULL); } ~critical_section() { pthread_mutex_destroy(&_cs); } void lock() const { pthread_mutex_lock(&_cs); } void unlock() const { pthread_mutex_unlock(&_cs); } bool trylock() const { return pthread_mutex_trylock(&_cs); } #elif defined(OS_WIN) critical_section() { InitializeCriticalSection(&_cs); } ~critical_section() { DeleteCriticalSection(&_cs); } void lock() const { EnterCriticalSection(&_cs); } void unlock() const { LeaveCriticalSection(&_cs); } bool trylock() const { TryEnterCriticalSection(&_cs); } #elif defined(OS_MBED) critical_section() { _count = 0; } ~critical_section() { } void lock() const { dint(); _count++; } void unlock() const { if(--_count<=0) { _count=0; eint(); } } bool trylock() const { dint(); _count++; return true; } #endif private: #ifdef OS_WIN mutable CRITICAL_SECTION _cs; #elif defined(OS_UNIX) mutable pthread_mutex_t _cs; #elif defined(OS_MBED) mutable short _count; #endif }; class cslock { public: explicit cslock(critical_section & cs) : _locked(false), _cs(cs) { ; } ~cslock() { if(_locked) unlock(); } void lock() { if(!_locked) _cs.lock(); _locked=true; } void unlock() { if(_locked) _cs.unlock(); _locked=false; } bool trylock() { if(!_locked) _locked=_cs.trylock(); return _locked; } private: explicit cslock() : _locked(false) { } explicit cslock(const cslock& l) : _locked(false) { } mutable bool _locked; mutable critical_section &_cs; // I edited this after posting, "&" }; }}