* [PATCH 01/10] usb-serial: URB write locking macros.
@ 2005-12-06 11:57 Luiz Fernando Capitulino
2005-12-06 12:24 ` Arjan van de Ven
2005-12-06 12:35 ` [linux-usb-devel] " Oliver Neukum
0 siblings, 2 replies; 5+ messages in thread
From: Luiz Fernando Capitulino @ 2005-12-06 11:57 UTC (permalink / raw)
To: gregkh; +Cc: linux-kernel, linux-usb-devel, ehabkost
Introduces URB write locking macros.
Signed-off-by: Luiz Capitulino <lcapitulino@mandriva.com.br>
drivers/usb/serial/usb-serial.h | 37 ++++++++++++++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff -Nparu -X /home/lcapitulino/kernels/dontdiff a/drivers/usb/serial/usb-serial.h a~/drivers/usb/serial/usb-serial.h
--- a/drivers/usb/serial/usb-serial.h 2005-12-04 20:05:26.000000000 -0200
+++ a~/drivers/usb/serial/usb-serial.h 2005-12-04 20:06:53.000000000 -0200
@@ -17,6 +17,7 @@
#include <linux/config.h>
#include <linux/kref.h>
#include <asm/semaphore.h>
+#include <asm/atomic.h>
#define SERIAL_TTY_MAJOR 188 /* Nice legal number now */
#define SERIAL_TTY_MINORS 255 /* loads of devices :) */
@@ -83,7 +84,7 @@ struct usb_serial_port {
unsigned char * bulk_out_buffer;
int bulk_out_size;
struct urb * write_urb;
- int write_urb_busy;
+ atomic_t write_urb_busy;
__u8 bulk_out_endpointAddress;
wait_queue_head_t write_wait;
@@ -104,6 +105,40 @@ static inline void usb_set_serial_port_d
dev_set_drvdata(&port->dev, data);
}
+/*
+ * usb_serial URB write access locking functions
+ *
+ * Protects 'write_urb_busy' member access, used to avoid two or more threads
+ * trying to write the same URB at the same time.
+ */
+
+/* Initialize the lock */
+static inline void usb_serial_write_urb_lock_init(struct usb_serial_port *port)
+{
+ atomic_set(&port->write_urb_busy, 0);
+}
+
+/*
+ * Lock function: returns zero if the lock was acquired,
+ * and non-zero otherwise
+ */
+static inline int usb_serial_write_urb_lock(struct usb_serial_port *port)
+{
+ return !atomic_add_unless(&port->write_urb_busy, 1, 1);
+}
+
+/* unlock function */
+static inline void usb_serial_write_urb_unlock(struct usb_serial_port *port)
+{
+ atomic_set(&port->write_urb_busy, 0);
+}
+
+/* Lock test: returns non-zero if the port is locked, and zero otherwise */
+static inline int usb_serial_write_urb_locked(struct usb_serial_port *port)
+{
+ return atomic_read(&port->write_urb_busy);
+}
+
/**
* usb_serial - structure used by the usb-serial core for a device
* @dev: pointer to the struct usb_device for this device
--
Luiz Fernando N. Capitulino
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 01/10] usb-serial: URB write locking macros.
2005-12-06 11:57 [PATCH 01/10] usb-serial: URB write locking macros Luiz Fernando Capitulino
@ 2005-12-06 12:24 ` Arjan van de Ven
2005-12-06 12:37 ` Luiz Fernando Capitulino
2005-12-06 12:46 ` Eduardo Pereira Habkost
2005-12-06 12:35 ` [linux-usb-devel] " Oliver Neukum
1 sibling, 2 replies; 5+ messages in thread
From: Arjan van de Ven @ 2005-12-06 12:24 UTC (permalink / raw)
To: Luiz Fernando Capitulino; +Cc: gregkh, linux-kernel, linux-usb-devel, ehabkost
On Tue, 2005-12-06 at 09:57 -0200, Luiz Fernando Capitulino wrote:
> Introduces URB write locking macros.
ugh.. WHY ?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 01/10] usb-serial: URB write locking macros.
2005-12-06 12:24 ` Arjan van de Ven
@ 2005-12-06 12:37 ` Luiz Fernando Capitulino
2005-12-06 12:46 ` Eduardo Pereira Habkost
1 sibling, 0 replies; 5+ messages in thread
From: Luiz Fernando Capitulino @ 2005-12-06 12:37 UTC (permalink / raw)
To: Arjan van de Ven; +Cc: gregkh, linux-kernel, linux-usb-devel, ehabkost
Hi Arjan,
On Tue, 06 Dec 2005 13:24:03 +0100
Arjan van de Ven <arjan@infradead.org> wrote:
| On Tue, 2005-12-06 at 09:57 -0200, Luiz Fernando Capitulino wrote:
| > Introduces URB write locking macros.
|
| ugh.. WHY ?
Because is easier to read/understand:
if (usb_serial_write_urb_lock(port)) {
dbg("%s - already writing", __FUNCTION__);
return 0;
}
than:
if (!atomic_add_unless(&port->write_urb_busy, 1, 1)) {
dbg("%s - already writing", __FUNCTION__);
return 0;
}
IMHO.
Of course I'm only ilustrating the 'lock' scenario, but you will have other
atomic functions spread in the driver.
Looks better to have some macros to make clear what you're doing.
--
Luiz Fernando N. Capitulino
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 01/10] usb-serial: URB write locking macros.
2005-12-06 12:24 ` Arjan van de Ven
2005-12-06 12:37 ` Luiz Fernando Capitulino
@ 2005-12-06 12:46 ` Eduardo Pereira Habkost
1 sibling, 0 replies; 5+ messages in thread
From: Eduardo Pereira Habkost @ 2005-12-06 12:46 UTC (permalink / raw)
To: Arjan van de Ven
Cc: Luiz Fernando Capitulino, gregkh, linux-kernel, linux-usb-devel,
Oliver Neukum
[-- Attachment #1: Type: text/plain, Size: 701 bytes --]
On Tue, Dec 06, 2005 at 01:24:03PM +0100, Arjan van de Ven wrote:
> On Tue, 2005-12-06 at 09:57 -0200, Luiz Fernando Capitulino wrote:
> > Introduces URB write locking macros.
>
> ugh.. WHY ?
Maybe we need a better description for the patch: the locking is already
there (the 'lock' field in struct sub_serial_port), and there is no
change of behaviour, just replacing a (spin_lock_t, int) by an atomic_t.
The purpose of the changes is removing the spinlock, because it is used
only to protect write_urb_busy, and if someday we need locking on other
parts, we already have a semaphore (introduced by Capitulino some time
ago, to fix a open()/close() race condition).
--
Eduardo
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [linux-usb-devel] [PATCH 01/10] usb-serial: URB write locking macros.
2005-12-06 11:57 [PATCH 01/10] usb-serial: URB write locking macros Luiz Fernando Capitulino
2005-12-06 12:24 ` Arjan van de Ven
@ 2005-12-06 12:35 ` Oliver Neukum
1 sibling, 0 replies; 5+ messages in thread
From: Oliver Neukum @ 2005-12-06 12:35 UTC (permalink / raw)
To: linux-usb-devel; +Cc: Luiz Fernando Capitulino, gregkh, linux-kernel, ehabkost
Am Dienstag, 6. Dezember 2005 12:57 schrieb Luiz Fernando Capitulino:
>
> Introduces URB write locking macros.
Bluntly put, what for? What good do these changes do?
Regards
Oliver
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-12-06 12:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-06 11:57 [PATCH 01/10] usb-serial: URB write locking macros Luiz Fernando Capitulino
2005-12-06 12:24 ` Arjan van de Ven
2005-12-06 12:37 ` Luiz Fernando Capitulino
2005-12-06 12:46 ` Eduardo Pereira Habkost
2005-12-06 12:35 ` [linux-usb-devel] " Oliver Neukum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®